Search in sources :

Example 11 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenTests method testNotEqualsDueToDifferentAuthenticationClass.

@Test
public void testNotEqualsDueToDifferentAuthenticationClass() {
    RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12);
    UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password", ROLES_12);
    assertThat(token1.equals(token2)).isFalse();
}
Also used : RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 12 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenTests method testEqualsWhenEqual.

@Test
public void testEqualsWhenEqual() {
    RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12);
    RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken("key", "Test", ROLES_12);
    assertThat(token2).isEqualTo(token1);
}
Also used : RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 13 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenMixinTests method deserializeRememberMeAuthenticationTokenWithUserTest.

@Test
public void deserializeRememberMeAuthenticationTokenWithUserTest() throws IOException {
    RememberMeAuthenticationToken token = mapper.readValue(String.format(REMEMBERME_AUTH_JSON, "\"password\""), RememberMeAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
    assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin");
    assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234");
    assertThat(((User) token.getPrincipal()).getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(((User) token.getPrincipal()).isEnabled()).isEqualTo(true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 14 with RememberMeAuthenticationToken

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

the class RememberMeAuthenticationTokenMixinTests method serializeRememberMeAuthenticationToken.

@Test
public void serializeRememberMeAuthenticationToken() throws JsonProcessingException, JSONException {
    RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, "admin", Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, actualJson, true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 15 with RememberMeAuthenticationToken

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

the class ExceptionTranslationFilterTests method testAccessDeniedWithRememberMe.

@Test
public void testAccessDeniedWithRememberMe() throws Exception {
    // Setup our HTTP request
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/secure/page.html");
    request.setServerPort(80);
    request.setScheme("http");
    request.setServerName("www.example.com");
    request.setContextPath("/mycontext");
    request.setRequestURI("/mycontext/secure/page.html");
    // Setup the FilterChain to thrown an access denied exception
    FilterChain fc = mock(FilterChain.class);
    doThrow(new AccessDeniedException("")).when(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
    // Setup SecurityContextHolder, as filter needs to check if user is remembered
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new RememberMeAuthenticationToken("ignored", "ignored", AuthorityUtils.createAuthorityList("IGNORED")));
    SecurityContextHolder.setContext(securityContext);
    // Test
    ExceptionTranslationFilter filter = new ExceptionTranslationFilter(mockEntryPoint);
    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/login.jsp");
    assertThat(getSavedRequestUrl(request)).isEqualTo("http://www.example.com/mycontext/secure/page.html");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) SecurityContext(org.springframework.security.core.context.SecurityContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) 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