Search in sources :

Example 66 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class JdbcUserDetailsManagerTests method createNewAuthenticationUsesNullPasswordToKeepPassordsSave.

// SEC-2166
@Test
public void createNewAuthenticationUsesNullPasswordToKeepPassordsSave() {
    insertJoe();
    UsernamePasswordAuthenticationToken currentAuth = new UsernamePasswordAuthenticationToken("joe", null, AuthorityUtils.createAuthorityList("ROLE_USER"));
    Authentication updatedAuth = manager.createNewAuthentication(currentAuth, "new");
    assertThat(updatedAuth.getCredentials()).isNull();
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 67 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class JdbcUserDetailsManagerTests method authenticateJoe.

private Authentication authenticateJoe() {
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("joe", "password", joe.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    return auth;
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 68 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class DefaultJaasAuthenticationProviderTests method setUp.

@Before
public void setUp() throws Exception {
    Configuration configuration = mock(Configuration.class);
    publisher = mock(ApplicationEventPublisher.class);
    log = mock(Log.class);
    provider = new DefaultJaasAuthenticationProvider();
    provider.setConfiguration(configuration);
    provider.setApplicationEventPublisher(publisher);
    provider.setAuthorityGranters(new AuthorityGranter[] { new TestAuthorityGranter() });
    provider.afterPropertiesSet();
    AppConfigurationEntry[] aces = new AppConfigurationEntry[] { new AppConfigurationEntry(TestLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, Collections.<String, Object>emptyMap()) };
    when(configuration.getAppConfigurationEntry(provider.getLoginContextName())).thenReturn(aces);
    token = new UsernamePasswordAuthenticationToken("user", "password");
    ReflectionTestUtils.setField(provider, "log", log);
}
Also used : DefaultJaasAuthenticationProvider(org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProvider) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) Configuration(javax.security.auth.login.Configuration) Log(org.apache.commons.logging.Log) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Before(org.junit.Before)

Example 69 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class DefaultJaasAuthenticationProviderTests method authenticateBadPassword.

@Test
public void authenticateBadPassword() {
    try {
        provider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf"));
        fail("LoginException should have been thrown for the bad password");
    } catch (AuthenticationException success) {
    }
    verifyFailedLogin();
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 70 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class SecurityContextHolderTests method testContextHolderGetterSetterClearer.

@Test
public void testContextHolderGetterSetterClearer() {
    SecurityContext sc = new SecurityContextImpl();
    sc.setAuthentication(new UsernamePasswordAuthenticationToken("Foobar", "pass"));
    SecurityContextHolder.setContext(sc);
    assertThat(SecurityContextHolder.getContext()).isEqualTo(sc);
    SecurityContextHolder.clearContext();
    assertThat(SecurityContextHolder.getContext()).isNotSameAs(sc);
    SecurityContextHolder.clearContext();
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)309 Test (org.junit.Test)156 Authentication (org.springframework.security.core.Authentication)114 GrantedAuthority (org.springframework.security.core.GrantedAuthority)37 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)34 UserDetails (org.springframework.security.core.userdetails.UserDetails)33 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 SecurityContext (org.springframework.security.core.context.SecurityContext)21 AuthenticationException (org.springframework.security.core.AuthenticationException)20 User (org.springframework.security.core.userdetails.User)17 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)15 ArrayList (java.util.ArrayList)14 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)13 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)12 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)11 Before (org.junit.Before)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8