Search in sources :

Example 21 with UserDetailsService

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

the class UserDetailsServiceFactoryBean method authenticationUserDetailsService.

@SuppressWarnings("unchecked")
AuthenticationUserDetailsService authenticationUserDetailsService(String name) {
    UserDetailsService uds;
    if (!StringUtils.hasText(name)) {
        Map<String, ?> beans = getBeansOfType(AuthenticationUserDetailsService.class);
        if (!beans.isEmpty()) {
            if (beans.size() > 1) {
                throw new ApplicationContextException("More than one AuthenticationUserDetailsService registered." + " Please use a specific Id reference.");
            }
            return (AuthenticationUserDetailsService) beans.values().toArray()[0];
        }
        uds = getUserDetailsService();
    } else {
        Object bean = beanFactory.getBean(name);
        if (bean instanceof AuthenticationUserDetailsService) {
            return (AuthenticationUserDetailsService) bean;
        } else if (bean instanceof UserDetailsService) {
            uds = cachingUserDetailsService(name);
            if (uds == null) {
                uds = (UserDetailsService) bean;
            }
        } else {
            throw new ApplicationContextException("Bean '" + name + "' must be a UserDetailsService or an" + " AuthenticationUserDetailsService");
        }
    }
    return new UserDetailsByNameServiceWrapper(uds);
}
Also used : CachingUserDetailsService(org.springframework.security.config.authentication.CachingUserDetailsService) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) UserDetailsByNameServiceWrapper(org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper) ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 22 with UserDetailsService

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

the class UserServiceBeanDefinitionParserTests method worksWithOpenIDUrlsAsNames.

@Test
public void worksWithOpenIDUrlsAsNames() {
    setContext("<user-service id='service'>" + "    <user name='http://joe.myopenid.com/' authorities='ROLE_A'/>" + "    <user name='https://www.google.com/accounts/o8/id?id=MPtOaenBIk5yzW9n7n9' authorities='ROLE_A'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    assertThat(userService.loadUserByUsername("http://joe.myopenid.com/").getUsername()).isEqualTo("http://joe.myopenid.com/");
    assertThat(userService.loadUserByUsername("https://www.google.com/accounts/o8/id?id=MPtOaenBIk5yzW9n7n9").getUsername()).isEqualTo("https://www.google.com/accounts/o8/id?id=MPtOaenBIk5yzW9n7n9");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 23 with UserDetailsService

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

the class UserServiceBeanDefinitionParserTests method userServiceWithValidPropertiesFileWorksSuccessfully.

@Test
public void userServiceWithValidPropertiesFileWorksSuccessfully() {
    setContext("<user-service id='service' " + "properties='classpath:org/springframework/security/config/users.properties'/>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    userService.loadUserByUsername("bob");
    userService.loadUserByUsername("joe");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 24 with UserDetailsService

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

the class DefaultUserAuthenticationConverterTests method shouldExtractAuthenticationWhenUserDetailsProvided.

@Test
public void shouldExtractAuthenticationWhenUserDetailsProvided() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(UserAuthenticationConverter.USERNAME, "test_user");
    UserDetailsService userDetailsService = Mockito.mock(UserDetailsService.class);
    Mockito.when(userDetailsService.loadUserByUsername("test_user")).thenReturn(new User("foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_SPAM")));
    converter.setUserDetailsService(userDetailsService);
    Authentication authentication = converter.extractAuthentication(map);
    assertEquals("ROLE_SPAM", authentication.getAuthorities().iterator().next().toString());
}
Also used : User(org.springframework.security.core.userdetails.User) HashMap(java.util.HashMap) Authentication(org.springframework.security.core.Authentication) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 25 with UserDetailsService

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

the class ManagementWebSecurityAutoConfigurationTests method getUser.

private UserDetails getUser() {
    ProviderManager parent = (ProviderManager) this.context.getBean(AuthenticationManager.class);
    DaoAuthenticationProvider provider = (DaoAuthenticationProvider) parent.getProviders().get(0);
    UserDetailsService service = (UserDetailsService) ReflectionTestUtils.getField(provider, "userDetailsService");
    UserDetails user = service.loadUserByUsername("user");
    return user;
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UserDetails(org.springframework.security.core.userdetails.UserDetails) DaoAuthenticationProvider(org.springframework.security.authentication.dao.DaoAuthenticationProvider) ProviderManager(org.springframework.security.authentication.ProviderManager) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService)

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