Search in sources :

Example 76 with UserDetails

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

the class JdbcUserDetailsManagerTests method changePasswordFailsIfReAuthenticationFails.

@Test
public void changePasswordFailsIfReAuthenticationFails() {
    insertJoe();
    authenticateJoe();
    AuthenticationManager am = mock(AuthenticationManager.class);
    when(am.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException(""));
    manager.setAuthenticationManager(am);
    try {
        manager.changePassword("password", "newPassword");
        fail("Expected BadCredentialsException");
    } catch (BadCredentialsException expected) {
    }
    // Check password hasn't changed.
    UserDetails newJoe = manager.loadUserByUsername("joe");
    assertThat(newJoe.getPassword()).isEqualTo("password");
    assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("password");
    assertThat(cache.getUserMap().containsKey("joe")).isTrue();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 77 with UserDetails

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

the class JdbcUserDetailsManagerTests method updateUserChangesDataCorrectlyAndClearsCache.

@Test
public void updateUserChangesDataCorrectlyAndClearsCache() {
    insertJoe();
    User newJoe = new User("joe", "newpassword", false, true, true, true, AuthorityUtils.createAuthorityList(new String[] { "D", "F", "E" }));
    manager.updateUser(newJoe);
    UserDetails joe = manager.loadUserByUsername("joe");
    assertThat(joe).isEqualTo(newJoe);
    assertThat(cache.getUserMap().containsKey("joe")).isFalse();
}
Also used : User(org.springframework.security.core.userdetails.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) Test(org.junit.Test)

Example 78 with UserDetails

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

the class JdbcUserDetailsManagerTests method createUserInsertsCorrectData.

@Test
public void createUserInsertsCorrectData() {
    manager.createUser(joe);
    UserDetails joe2 = manager.loadUserByUsername("joe");
    assertThat(joe2).isEqualTo(joe);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Test(org.junit.Test)

Example 79 with UserDetails

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

the class LdapAuthenticationProviderTests method useWithNullAuthoritiesPopulatorReturnsCorrectRole.

@Test
public void useWithNullAuthoritiesPopulatorReturnsCorrectRole() {
    LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator());
    LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
    userMapper.setRoleAttributes(new String[] { "ou" });
    ldapProvider.setUserDetailsContextMapper(userMapper);
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben", "benspassword");
    UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest).getPrincipal();
    assertThat(user.getAuthorities()).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 80 with UserDetails

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

the class OpenIDAuthenticationProviderTests method testAuthenticateSuccess.

/*
	 * Test method for
	 * 'org.springframework.security.authentication.openid.OpenIDAuthenticationProvider.
	 * authenticate(Authentication)'
	 */
@Test
public void testAuthenticateSuccess() {
    OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
    provider.setUserDetailsService(new MockUserDetailsService());
    Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SUCCESS, USERNAME, "", null);
    assertThat(preAuth.isAuthenticated()).isFalse();
    Authentication postAuth = provider.authenticate(preAuth);
    assertThat(postAuth).isNotNull();
    assertThat(postAuth instanceof OpenIDAuthenticationToken).isTrue();
    assertThat(postAuth.isAuthenticated()).isTrue();
    assertThat(postAuth.getPrincipal()).isNotNull();
    assertThat(postAuth.getPrincipal() instanceof UserDetails).isTrue();
    assertThat(postAuth.getAuthorities()).isNotNull();
    assertThat(postAuth.getAuthorities().size() > 0).isTrue();
    assertThat(((OpenIDAuthenticationToken) postAuth).getStatus() == OpenIDAuthenticationStatus.SUCCESS).isTrue();
    assertThat(((OpenIDAuthenticationToken) postAuth).getMessage() == null).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)111 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)33 Authentication (org.springframework.security.core.Authentication)32 GrantedAuthority (org.springframework.security.core.GrantedAuthority)17 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)15 User (org.springframework.security.core.userdetails.User)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)10 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 Transactional (org.springframework.transaction.annotation.Transactional)6 Date (java.util.Date)4 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 AuthenticationException (org.springframework.security.core.AuthenticationException)4