Search in sources :

Example 11 with UserDetails

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

the class LdapUserDetailsServiceTests method nullPopulatorConstructorReturnsEmptyAuthoritiesList.

@Test
public void nullPopulatorConstructorReturnsEmptyAuthoritiesList() throws Exception {
    DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
    LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData));
    UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
    assertThat(user.getAuthorities()).isEmpty();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) DistinguishedName(org.springframework.ldap.core.DistinguishedName) MockUserSearch(org.springframework.security.ldap.authentication.MockUserSearch) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 12 with UserDetails

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

the class LdapUserDetailsServiceTests method correctAuthoritiesAreReturned.

@Test
public void correctAuthoritiesAreReturned() {
    DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
    LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData), new MockAuthoritiesPopulator());
    service.setUserDetailsMapper(new LdapUserDetailsMapper());
    UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
    Set<String> authorities = AuthorityUtils.authorityListToSet(user.getAuthorities());
    assertThat(authorities).hasSize(1);
    assertThat(authorities.contains("ROLE_FROM_POPULATOR")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) DistinguishedName(org.springframework.ldap.core.DistinguishedName) MockUserSearch(org.springframework.security.ldap.authentication.MockUserSearch) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 13 with UserDetails

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

the class PreAuthenticatedAuthenticationProviderTests method authenticateUnknownUserThrowsException.

@Test(expected = UsernameNotFoundException.class)
public final void authenticateUnknownUserThrowsException() throws Exception {
    UserDetails ud = new User("dummyUser1", "dummyPwd", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new PreAuthenticatedAuthenticationToken("dummyUser2", "dummyPwd");
    provider.authenticate(request);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Example 14 with UserDetails

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

the class PreAuthenticatedAuthenticationProviderTests method authenticateInvalidToken.

@Test
public final void authenticateInvalidToken() throws Exception {
    UserDetails ud = new User("dummyUser", "dummyPwd", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
    PreAuthenticatedAuthenticationProvider provider = getProvider(ud);
    Authentication request = new UsernamePasswordAuthenticationToken("dummyUser", "dummyPwd");
    Authentication result = provider.authenticate(request);
    assertThat(result).isNull();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 15 with UserDetails

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

the class TokenBasedRememberMeServices method onLoginSuccess.

@Override
public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
    String username = retrieveUserName(successfulAuthentication);
    String password = retrievePassword(successfulAuthentication);
    // unable to construct a valid token in this case.
    if (!StringUtils.hasLength(username)) {
        logger.debug("Unable to retrieve username");
        return;
    }
    if (!StringUtils.hasLength(password)) {
        UserDetails user = getUserDetailsService().loadUserByUsername(username);
        password = user.getPassword();
        if (!StringUtils.hasLength(password)) {
            logger.debug("Unable to obtain password for user: " + username);
            return;
        }
    }
    int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication);
    long expiryTime = System.currentTimeMillis();
    // SEC-949
    expiryTime += 1000L * (tokenLifetime < 0 ? TWO_WEEKS_S : tokenLifetime);
    String signatureValue = makeTokenSignature(expiryTime, username, password);
    setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response);
    if (logger.isDebugEnabled()) {
        logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'");
    }
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Date(java.util.Date)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)97 Test (org.junit.Test)37 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)32 Authentication (org.springframework.security.core.Authentication)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 User (org.springframework.security.core.userdetails.User)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)9 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)3