Search in sources :

Example 71 with SimpleGrantedAuthority

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();
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) Principal(java.security.Principal) Test(org.junit.Test)

Example 72 with SimpleGrantedAuthority

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");
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Health(org.springframework.boot.actuate.health.Health) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Example 73 with SimpleGrantedAuthority

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);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority)

Example 74 with SimpleGrantedAuthority

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;
}
Also used : Set(java.util.Set) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 75 with SimpleGrantedAuthority

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;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority)

Aggregations

SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)109 GrantedAuthority (org.springframework.security.core.GrantedAuthority)64 Test (org.junit.Test)49 ArrayList (java.util.ArrayList)30 Authentication (org.springframework.security.core.Authentication)27 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)22 HashSet (java.util.HashSet)14 User (org.springframework.security.core.userdetails.User)11 ObjectIdentity (org.springframework.security.acls.model.ObjectIdentity)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)7 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)7 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)7 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)7 Before (org.junit.Before)6 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)6 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)6 List (java.util.List)5 Map (java.util.Map)5 DBUnitTest (org.orcid.test.DBUnitTest)5