Search in sources :

Example 1 with RememberMeAuthenticationToken

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

the class AbstractRememberMeServices method createSuccessfulAuthentication.

/**
	 * Creates the final <tt>Authentication</tt> object returned from the
	 * <tt>autoLogin</tt> method.
	 * <p>
	 * By default it will create a <tt>RememberMeAuthenticationToken</tt> instance.
	 *
	 * @param request the original request. The configured
	 * <tt>AuthenticationDetailsSource</tt> will use this to build the details property of
	 * the returned object.
	 * @param user the <tt>UserDetails</tt> loaded from the <tt>UserDetailsService</tt>.
	 * This will be stored as the principal.
	 *
	 * @return the <tt>Authentication</tt> for the remember-me authenticated user
	 */
protected Authentication createSuccessfulAuthentication(HttpServletRequest request, UserDetails user) {
    RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(key, user, authoritiesMapper.mapAuthorities(user.getAuthorities()));
    auth.setDetails(authenticationDetailsSource.buildDetails(request));
    return auth;
}
Also used : RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken)

Example 2 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenMixinTests method serializeRememberMeAuthenticationWithUserToken.

@Test
public void serializeRememberMeAuthenticationWithUserToken() throws JsonProcessingException, JSONException {
    User user = createDefaultUser();
    RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, user, user.getAuthorities());
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(String.format(REMEMBERME_AUTH_JSON, "\"password\""), actualJson, true);
}
Also used : User(org.springframework.security.core.userdetails.User) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 3 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenMixinTests method deserializeRememberMeAuthenticationToken.

@Test
public void deserializeRememberMeAuthenticationToken() throws IOException {
    RememberMeAuthenticationToken token = mapper.readValue(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, RememberMeAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isEqualTo("admin").isEqualTo(token.getName());
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 4 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenMixinTests method serializeRememberMeAuthenticationWithUserTokenAfterEraseCredential.

@Test
public void serializeRememberMeAuthenticationWithUserTokenAfterEraseCredential() throws JsonProcessingException, JSONException {
    User user = createDefaultUser();
    RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, user, user.getAuthorities());
    token.eraseCredentials();
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(REMEMBERME_AUTH_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
}
Also used : User(org.springframework.security.core.userdetails.User) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 5 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationProviderTests method testNormalOperation.

@Test
public void testNormalOperation() throws Exception {
    RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
    RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("qwerty", "Test", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
    Authentication result = aap.authenticate(token);
    assertThat(token).isEqualTo(result);
}
Also used : RememberMeAuthenticationProvider(org.springframework.security.authentication.RememberMeAuthenticationProvider) Authentication(org.springframework.security.core.Authentication) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Aggregations

RememberMeAuthenticationToken (org.springframework.security.authentication.RememberMeAuthenticationToken)15 Test (org.junit.Test)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 User (org.springframework.security.core.userdetails.User)3 RememberMeAuthenticationProvider (org.springframework.security.authentication.RememberMeAuthenticationProvider)2 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1