Search in sources :

Example 16 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security-oauth by spring-projects.

the class Role method translate.

private Collection<? extends GrantedAuthority> translate(List<Role> roles) {
    List<GrantedAuthority> authorities = new ArrayList<>();
    for (Role role : roles) {
        String name = role.getName().toUpperCase();
        if (!name.startsWith("ROLE_")) {
            name = "ROLE_" + name;
        }
        authorities.add(new SimpleGrantedAuthority(name));
    }
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 17 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project camel by apache.

the class SpringSecurityAuthorizationPolicyTest method createAuthenticationToken.

private Authentication createAuthenticationToken(String username, String password, String... roles) {
    Authentication authToken;
    if (roles != null && roles.length > 0) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(roles.length);
        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        authToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
    } else {
        authToken = new UsernamePasswordAuthenticationToken(username, password);
    }
    return authToken;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 18 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class SidTests method testGrantedAuthoritySidHashCode.

@Test
public void testGrantedAuthoritySidHashCode() throws Exception {
    GrantedAuthority ga = new SimpleGrantedAuthority("ROLE_TEST");
    Sid gaSid = new GrantedAuthoritySid(ga);
    assertThat(gaSid.hashCode()).isEqualTo("ROLE_TEST".hashCode());
    assertThat(gaSid.hashCode()).isEqualTo(new GrantedAuthoritySid("ROLE_TEST").hashCode());
    assertThat(gaSid.hashCode()).isNotEqualTo(new GrantedAuthoritySid("ROLE_TEST_2").hashCode());
    assertThat(gaSid.hashCode()).isNotEqualTo(new GrantedAuthoritySid(new SimpleGrantedAuthority("ROLE_TEST_2")).hashCode());
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Sid(org.springframework.security.acls.model.Sid) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) Test(org.junit.Test)

Example 19 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class SidTests method testGrantedAuthoritySidConstructorsRequiredFields.

@Test
public void testGrantedAuthoritySidConstructorsRequiredFields() throws Exception {
    // Check one String-argument constructor
    try {
        String string = null;
        new GrantedAuthoritySid(string);
        fail("It should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        new GrantedAuthoritySid("");
        fail("It should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        new GrantedAuthoritySid("ROLE_TEST");
    } catch (IllegalArgumentException notExpected) {
        fail("It shouldn't have thrown IllegalArgumentException");
    }
    // Check one GrantedAuthority-argument constructor
    try {
        GrantedAuthority ga = null;
        new GrantedAuthoritySid(ga);
        fail("It should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        GrantedAuthority ga = new SimpleGrantedAuthority(null);
        new GrantedAuthoritySid(ga);
        fail("It should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        GrantedAuthority ga = new SimpleGrantedAuthority("ROLE_TEST");
        new GrantedAuthoritySid(ga);
    } catch (IllegalArgumentException notExpected) {
        fail("It shouldn't have thrown IllegalArgumentException");
    }
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Example 20 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-security by spring-projects.

the class SidTests method testGrantedAuthoritySidEquals.

@Test
public void testGrantedAuthoritySidEquals() throws Exception {
    GrantedAuthority ga = new SimpleGrantedAuthority("ROLE_TEST");
    Sid gaSid = new GrantedAuthoritySid(ga);
    assertThat(gaSid.equals(null)).isFalse();
    assertThat(gaSid.equals("DIFFERENT_TYPE_OBJECT")).isFalse();
    assertThat(gaSid.equals(gaSid)).isTrue();
    assertThat(gaSid.equals(new GrantedAuthoritySid(ga))).isTrue();
    assertThat(gaSid.equals(new GrantedAuthoritySid(new SimpleGrantedAuthority("ROLE_TEST")))).isTrue();
    assertThat(gaSid.equals(new GrantedAuthoritySid(new SimpleGrantedAuthority("ROLE_NOT_EQUAL")))).isFalse();
    assertThat(gaSid.equals(new GrantedAuthoritySid("ROLE_TEST"))).isTrue();
    assertThat(gaSid.equals(new GrantedAuthoritySid("ROLE_NOT_EQUAL"))).isFalse();
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Sid(org.springframework.security.acls.model.Sid) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) Test(org.junit.Test)

Aggregations

GrantedAuthority (org.springframework.security.core.GrantedAuthority)188 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)90 Authentication (org.springframework.security.core.Authentication)55 ArrayList (java.util.ArrayList)43 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)37 HashSet (java.util.HashSet)27 UserDetails (org.springframework.security.core.userdetails.UserDetails)16 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)15 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)11 Before (org.junit.Before)10 SecurityContext (org.springframework.security.core.context.SecurityContext)10 User (org.springframework.security.core.userdetails.User)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 DefaultGrantedAuthority (eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority)9 List (java.util.List)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)9 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8