Search in sources :

Example 16 with UserDetailsService

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

the class LdapUserServiceBeanDefinitionParserTests method rolePrefixIsSupported.

@Test
public void rolePrefixIsSupported() throws Exception {
    setContext("<ldap-user-service id='ldapUDS' " + "     user-search-filter='(uid={0})' " + "     group-search-filter='member={0}' role-prefix='PREFIX_'/>" + "<ldap-user-service id='ldapUDSNoPrefix' " + "     user-search-filter='(uid={0})' " + "     group-search-filter='member={0}' role-prefix='none'/><ldap-server ldif='classpath:test-server.ldif'/>");
    UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS");
    UserDetails ben = uds.loadUserByUsername("ben");
    assertThat(AuthorityUtils.authorityListToSet(ben.getAuthorities())).contains("PREFIX_DEVELOPERS");
    uds = (UserDetailsService) appCtx.getBean("ldapUDSNoPrefix");
    ben = uds.loadUserByUsername("ben");
    assertThat(AuthorityUtils.authorityListToSet(ben.getAuthorities())).contains("DEVELOPERS");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) Test(org.junit.Test)

Example 17 with UserDetailsService

use of org.springframework.security.core.userdetails.UserDetailsService in project ocvn by devgateway.

the class TestUserDetailsConfiguration method testUserDetailsAdminProcuringEntity.

@Bean("testUserDetailsAdminProcuringEntity")
public UserDetailsService testUserDetailsAdminProcuringEntity() {
    return new UserDetailsService() {

        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            Person person = new Person();
            person.setUsername(username);
            person.setPassword("idontcare");
            person.setAuthorities(Arrays.asList(new SimpleGrantedAuthority("ROLE_PROCURING_ENTITY"), new SimpleGrantedAuthority("ROLE_ADMIN")));
            return personRepository.save(person);
        }
    };
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Person(org.devgateway.toolkit.persistence.dao.Person) Bean(org.springframework.context.annotation.Bean)

Example 18 with UserDetailsService

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

the class LdapUserServiceBeanDefinitionParserTests method externalContextMapperIsSupported.

@Test
public void externalContextMapperIsSupported() {
    setContext("<ldap-server id='someServer' ldif='classpath:test-server.ldif'/>" + "<ldap-user-service id='ldapUDS' user-search-filter='(uid={0})' user-context-mapper-ref='mapper'/>" + "<b:bean id='mapper' class='" + InetOrgPersonContextMapper.class.getName() + "'/>");
    UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS");
    UserDetails ben = uds.loadUserByUsername("ben");
    assertThat(ben instanceof InetOrgPerson).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) InetOrgPerson(org.springframework.security.ldap.userdetails.InetOrgPerson) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) InetOrgPersonContextMapper(org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper) Test(org.junit.Test)

Example 19 with UserDetailsService

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

the class LdapUserServiceBeanDefinitionParserTests method userServiceReturnsExpectedData.

@Test
public void userServiceReturnsExpectedData() throws Exception {
    setContext("<ldap-user-service id='ldapUDS' user-search-filter='(uid={0})' group-search-filter='member={0}' /><ldap-server ldif='classpath:test-server.ldif'/>");
    UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS");
    UserDetails ben = uds.loadUserByUsername("ben");
    Set<String> authorities = AuthorityUtils.authorityListToSet(ben.getAuthorities());
    assertThat(authorities).hasSize(3);
    assertThat(authorities.contains("ROLE_DEVELOPERS")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) Test(org.junit.Test)

Example 20 with UserDetailsService

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

the class LdapUserServiceBeanDefinitionParserTests method personContextMapperIsSupported.

@Test
public void personContextMapperIsSupported() {
    setContext("<ldap-server ldif='classpath:test-server.ldif'/>" + "<ldap-user-service id='ldapUDS' user-search-filter='(uid={0})' user-details-class='person'/>");
    UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS");
    UserDetails ben = uds.loadUserByUsername("ben");
    assertThat(ben instanceof Person).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) Person(org.springframework.security.ldap.userdetails.Person) InetOrgPerson(org.springframework.security.ldap.userdetails.InetOrgPerson) Test(org.junit.Test)

Aggregations

UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)26 Test (org.junit.Test)19 UserDetails (org.springframework.security.core.userdetails.UserDetails)14 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 User (org.springframework.security.core.userdetails.User)3 InetOrgPerson (org.springframework.security.ldap.userdetails.InetOrgPerson)3 Before (org.junit.Before)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Person (org.devgateway.toolkit.persistence.dao.Person)1 ApplicationContextException (org.springframework.context.ApplicationContextException)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)1 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)1 ProviderManager (org.springframework.security.authentication.ProviderManager)1 DaoAuthenticationProvider (org.springframework.security.authentication.dao.DaoAuthenticationProvider)1