Search in sources :

Example 36 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) this.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.jupiter.api.Test)

Example 37 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() {
    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) this.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.jupiter.api.Test)

Example 38 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) this.appContext.getBean("service");
    userService.loadUserByUsername("bob");
    userService.loadUserByUsername("joe");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.jupiter.api.Test)

Example 39 with UserDetailsService

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

the class WebSecurityConfigurerAdapterTests method loadConfigWhenInMemoryConfigureGlobalThenPasswordUpgraded.

@Test
public void loadConfigWhenInMemoryConfigureGlobalThenPasswordUpgraded() throws Exception {
    this.spring.register(InMemoryConfigureGlobalConfig.class).autowire();
    this.mockMvc.perform(formLogin()).andExpect(status().is3xxRedirection());
    UserDetailsService uds = this.spring.getContext().getBean(UserDetailsService.class);
    assertThat(uds.loadUserByUsername("user").getPassword()).startsWith("{bcrypt}");
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.jupiter.api.Test)

Example 40 with UserDetailsService

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

the class AuthenticationConfigurationTests method getAuthenticationWhenUserDetailsServiceBeanThenAuthenticationManagerUsesUserDetailsServiceBean.

// sec-2868
@Test
public void getAuthenticationWhenUserDetailsServiceBeanThenAuthenticationManagerUsesUserDetailsServiceBean() throws Exception {
    this.spring.register(UserDetailsServiceBeanConfig.class).autowire();
    UserDetailsService uds = this.spring.getContext().getBean(UserDetailsService.class);
    AuthenticationManager am = this.spring.getContext().getBean(AuthenticationConfiguration.class).getAuthenticationManager();
    given(uds.loadUserByUsername("user")).willReturn(PasswordEncodedUser.user(), PasswordEncodedUser.user());
    am.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
    assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> am.authenticate(new UsernamePasswordAuthenticationToken("user", "invalid")));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)44 Test (org.junit.jupiter.api.Test)26 UserDetails (org.springframework.security.core.userdetails.UserDetails)23 Test (org.junit.Test)7 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)7 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 User (org.springframework.security.core.userdetails.User)6 Authentication (org.springframework.security.core.Authentication)5 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)5 Bean (org.springframework.context.annotation.Bean)4 List (java.util.List)3 UserDetailsPasswordService (org.springframework.security.core.userdetails.UserDetailsPasswordService)3 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)3 NoOpPasswordEncoder (org.springframework.security.crypto.password.NoOpPasswordEncoder)3 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Person (org.devgateway.toolkit.persistence.dao.Person)2 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)2 ITenant (org.pentaho.platform.api.mt.ITenant)2