use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-boot by spring-projects.
the class MvcEndpointSecurityInterceptorTests method sensitiveEndpointIfRoleNotCorrectShouldCheckAuthorities.
@Test
public void sensitiveEndpointIfRoleNotCorrectShouldCheckAuthorities() throws Exception {
Principal principal = mock(Principal.class);
this.request.setUserPrincipal(principal);
Authentication authentication = mock(Authentication.class);
Set<SimpleGrantedAuthority> authorities = Collections.singleton(new SimpleGrantedAuthority("SUPER_HERO"));
doReturn(authorities).when(authentication).getAuthorities();
SecurityContextHolder.getContext().setAuthentication(authentication);
assertThat(this.securityInterceptor.preHandle(this.request, this.response, this.handlerMethod)).isTrue();
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-boot by spring-projects.
the class HealthMvcEndpointTests method rightAuthorityPresentShouldExposeDetails.
@Test
public void rightAuthorityPresentShouldExposeDetails() throws Exception {
this.environment.getPropertySources().addLast(SECURITY_ROLES);
Authentication principal = mock(Authentication.class);
Set<SimpleGrantedAuthority> authorities = Collections.singleton(new SimpleGrantedAuthority("HERO"));
doReturn(authorities).when(principal).getAuthorities();
given(this.endpoint.invoke()).willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
Object result = this.mvc.invoke(this.defaultUser, principal);
assertThat(result instanceof Health).isTrue();
assertThat(((Health) result).getStatus() == Status.UP).isTrue();
assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-security by spring-projects.
the class SimpleAuthorityMapper method setDefaultAuthority.
/**
* Sets a default authority to be assigned to all users
*
* @param authority the name of the authority to be assigned to all users.
*/
public void setDefaultAuthority(String authority) {
Assert.hasText(authority, "The authority name cannot be set to an empty value");
this.defaultAuthority = new SimpleGrantedAuthority(authority);
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-security by spring-projects.
the class UserDeserializer method deserialize.
/**
* This method will create {@link User} object. It will ensure successful object creation even if password key is null in
* serialized json, because credentials may be removed from the {@link User} by invoking {@link User#eraseCredentials()}.
* In that case there won't be any password key in serialized json.
*
* @param jp the JsonParser
* @param ctxt the DeserializationContext
* @return the user
* @throws IOException if a exception during IO occurs
* @throws JsonProcessingException if an error during JSON processing occurs
*/
@Override
public User deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode jsonNode = mapper.readTree(jp);
Set<GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"), new TypeReference<Set<SimpleGrantedAuthority>>() {
});
JsonNode password = readJsonNode(jsonNode, "password");
User result = new User(readJsonNode(jsonNode, "username").asText(), password.asText(""), readJsonNode(jsonNode, "enabled").asBoolean(), readJsonNode(jsonNode, "accountNonExpired").asBoolean(), readJsonNode(jsonNode, "credentialsNonExpired").asBoolean(), readJsonNode(jsonNode, "accountNonLocked").asBoolean(), authorities);
if (password.asText(null) == null) {
result.eraseCredentials();
}
return result;
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project spring-security by spring-projects.
the class MapBasedAttributes2GrantedAuthoritiesMapperTests method getValidAttributes2GrantedAuthoritiesMap.
private HashMap getValidAttributes2GrantedAuthoritiesMap() {
HashMap m = new HashMap();
m.put("role1", "ga1");
m.put("role2", new SimpleGrantedAuthority("ga2"));
m.put("role3", Arrays.asList("ga3", new SimpleGrantedAuthority("ga4")));
m.put("role4", "ga5,ga6");
m.put("role5", Arrays.asList("ga7", "ga8", new Object[] { new SimpleGrantedAuthority("ga9") }));
m.put("role6", new Object[] { "ga10", "ga11", new Object[] { new SimpleGrantedAuthority("ga12") } });
m.put("role7", new String[] { "ga13", "ga14" });
m.put("role8", new String[] { "ga13", "ga14", null });
m.put("role9", null);
m.put("role10", new Object[] {});
m.put("role11", Arrays.asList(new Object[] { null }));
return m;
}
Aggregations