Search in sources :

Example 6 with UserDetailsService

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

the class UserServiceBeanDefinitionParserTests method disabledAndEmbeddedFlagsAreSupported.

@Test
public void disabledAndEmbeddedFlagsAreSupported() {
    setContext("<user-service id='service'>" + "    <user name='joe' password='joespassword' authorities='ROLE_A' locked='true'/>" + "    <user name='Bob' password='bobspassword' authorities='ROLE_A' disabled='true'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    UserDetails joe = userService.loadUserByUsername("joe");
    assertThat(joe.isAccountNonLocked()).isFalse();
    // Check case-sensitive lookup SEC-1432
    UserDetails bob = userService.loadUserByUsername("Bob");
    assertThat(bob.isEnabled()).isFalse();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 7 with UserDetailsService

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

the class UserServiceBeanDefinitionParserTests method userWithBothPropertiesAndEmbeddedUsersThrowsException.

@Test(expected = FatalBeanException.class)
public void userWithBothPropertiesAndEmbeddedUsersThrowsException() {
    setContext("<user-service id='service' properties='doesntmatter.props'>" + "    <user name='joe' password='joespassword' authorities='ROLE_A'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    userService.loadUserByUsername("Joe");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 8 with UserDetailsService

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

the class GlobalMethodSecurityBeanDefinitionParserTests method worksWithAspectJAutoproxy.

@Test(expected = AccessDeniedException.class)
public void worksWithAspectJAutoproxy() {
    setContext("<global-method-security>" + "  <protect-pointcut expression='execution(* org.springframework.security.config.*Service.*(..))'" + "       access='ROLE_SOMETHING' />" + "</global-method-security>" + "<b:bean id='myUserService' class='org.springframework.security.config.PostProcessedMockUserDetailsService'/>" + "<aop:aspectj-autoproxy />" + "<authentication-manager>" + "   <authentication-provider user-service-ref='myUserService'/>" + "</authentication-manager>");
    UserDetailsService service = (UserDetailsService) appContext.getBean("myUserService");
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ROLE_SOMEOTHERROLE"));
    SecurityContextHolder.getContext().setAuthentication(token);
    service.loadUserByUsername("notused");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) PostProcessedMockUserDetailsService(org.springframework.security.config.PostProcessedMockUserDetailsService) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 9 with UserDetailsService

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

the class UserDetailsServiceLdapAuthoritiesPopulatorTests method delegationToUserDetailsServiceReturnsCorrectRoles.

@Test
public void delegationToUserDetailsServiceReturnsCorrectRoles() throws Exception {
    UserDetailsService uds = mock(UserDetailsService.class);
    UserDetails user = mock(UserDetails.class);
    when(uds.loadUserByUsername("joe")).thenReturn(user);
    List authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
    when(user.getAuthorities()).thenReturn(authorities);
    UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
    Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(new DirContextAdapter(), "joe");
    assertThat(auths).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(auths).contains("ROLE_USER")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsServiceLdapAuthoritiesPopulator(org.springframework.security.ldap.authentication.UserDetailsServiceLdapAuthoritiesPopulator) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) List(java.util.List) Test(org.junit.Test)

Example 10 with UserDetailsService

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

the class DigestAuthenticationFilterTests method setUp.

@Before
public void setUp() {
    SecurityContextHolder.clearContext();
    // Create User Details Service
    UserDetailsService uds = new UserDetailsService() {

        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User("rod,ok", "koala", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
        }
    };
    DigestAuthenticationEntryPoint ep = new DigestAuthenticationEntryPoint();
    ep.setRealmName(REALM);
    ep.setKey(KEY);
    filter = new DigestAuthenticationFilter();
    filter.setUserDetailsService(uds);
    filter.setAuthenticationEntryPoint(ep);
    request = new MockHttpServletRequest("GET", REQUEST_URI);
    request.setServletPath(REQUEST_URI);
}
Also used : User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Before(org.junit.Before)

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