Search in sources :

Example 36 with GrantedAuthority

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

the class DefaultLdapAuthoritiesPopulatorTests method defaultRoleIsAssignedWhenSet.

@Test
public void defaultRoleIsAssignedWhenSet() {
    populator.setDefaultRole("ROLE_USER");
    assertThat(populator.getContextSource()).isSameAs(getContextSource());
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("cn=notfound"));
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "notfound");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 37 with GrantedAuthority

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

the class DefaultLdapAuthoritiesPopulatorTests method nullSearchBaseIsAccepted.

@Test
public void nullSearchBaseIsAccepted() throws Exception {
    populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), null);
    populator.setDefaultRole("ROLE_USER");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 38 with GrantedAuthority

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

the class DefaultLdapAuthoritiesPopulatorTests method extraRolesAreAdded.

@Test
public void extraRolesAreAdded() throws Exception {
    populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), null) {

        @Override
        protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, String username) {
            return new HashSet<GrantedAuthority>(AuthorityUtils.createAuthorityList("ROLE_EXTRA"));
        }
    };
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_EXTRA")).isTrue();
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 39 with GrantedAuthority

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

the class NestedLdapAuthoritiesPopulatorTests method testScalaDudeJDevelopersAuthoritiesWithSearchLimit.

@Test
public void testScalaDudeJDevelopersAuthoritiesWithSearchLimit() {
    populator.setMaxSearchDepth(1);
    DirContextAdapter ctx = new DirContextAdapter("uid=scaladude,ou=people,dc=springframework,dc=org");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "scaladude");
    assertThat(authorities).hasSize(1);
    assertThat(authorities).isEqualTo(Arrays.asList(scalaDevelopers));
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 40 with GrantedAuthority

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

the class NestedLdapAuthoritiesPopulatorTests method testJavaDudeJDevelopersAuthorities.

@Test
public void testJavaDudeJDevelopersAuthorities() {
    DirContextAdapter ctx = new DirContextAdapter("uid=javadude,ou=people,dc=springframework,dc=org");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "javadude");
    assertThat(authorities).hasSize(3);
    assertThat(authorities).contains(javaDevelopers);
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) 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