|
27 | 27 | import org.junit.jupiter.api.AfterEach;
|
28 | 28 | import org.junit.jupiter.api.BeforeEach;
|
29 | 29 | import org.junit.jupiter.api.Test;
|
| 30 | +import org.mockito.ArgumentCaptor; |
30 | 31 |
|
31 | 32 | import org.springframework.mock.web.MockFilterConfig;
|
32 | 33 | import org.springframework.mock.web.MockHttpServletRequest;
|
33 | 34 | import org.springframework.mock.web.MockHttpServletResponse;
|
34 | 35 | import org.springframework.security.authentication.AuthenticationManager;
|
35 | 36 | import org.springframework.security.authentication.BadCredentialsException;
|
36 | 37 | import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
| 38 | +import org.springframework.security.authentication.TestAuthentication; |
37 | 39 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
38 | 40 | import org.springframework.security.core.Authentication;
|
39 | 41 | import org.springframework.security.core.AuthenticationException;
|
40 | 42 | import org.springframework.security.core.authority.AuthorityUtils;
|
| 43 | +import org.springframework.security.core.context.SecurityContext; |
41 | 44 | import org.springframework.security.core.context.SecurityContextHolder;
|
42 | 45 | import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServicesTests;
|
43 | 46 | import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
|
44 | 47 | import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
| 48 | +import org.springframework.security.web.context.SecurityContextRepository; |
45 | 49 | import org.springframework.security.web.firewall.DefaultHttpFirewall;
|
46 | 50 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
47 | 51 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
@@ -322,6 +326,37 @@ public void testSuccessfulAuthenticationInvokesSuccessHandlerAndSetsContext() th
|
322 | 326 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
|
323 | 327 | }
|
324 | 328 |
|
| 329 | + @Test |
| 330 | + public void testSuccessfulAuthenticationThenDefaultDoesNotCreateSession() throws Exception { |
| 331 | + Authentication authentication = TestAuthentication.authenticatedUser(); |
| 332 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 333 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 334 | + MockFilterChain chain = new MockFilterChain(false); |
| 335 | + MockAuthenticationFilter filter = new MockAuthenticationFilter(); |
| 336 | + |
| 337 | + filter.successfulAuthentication(request, response, chain, authentication); |
| 338 | + |
| 339 | + assertThat(request.getSession(false)).isNull(); |
| 340 | + } |
| 341 | + |
| 342 | + @Test |
| 343 | + public void testSuccessfulAuthenticationWhenCustomSecurityContextRepositoryThenAuthenticationSaved() |
| 344 | + throws Exception { |
| 345 | + ArgumentCaptor<SecurityContext> contextCaptor = ArgumentCaptor.forClass(SecurityContext.class); |
| 346 | + SecurityContextRepository repository = mock(SecurityContextRepository.class); |
| 347 | + Authentication authentication = TestAuthentication.authenticatedUser(); |
| 348 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 349 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 350 | + MockFilterChain chain = new MockFilterChain(false); |
| 351 | + MockAuthenticationFilter filter = new MockAuthenticationFilter(); |
| 352 | + filter.setSecurityContextRepository(repository); |
| 353 | + |
| 354 | + filter.successfulAuthentication(request, response, chain, authentication); |
| 355 | + |
| 356 | + verify(repository).saveContext(contextCaptor.capture(), eq(request), eq(response)); |
| 357 | + assertThat(contextCaptor.getValue().getAuthentication()).isEqualTo(authentication); |
| 358 | + } |
| 359 | + |
325 | 360 | @Test
|
326 | 361 | public void testFailedAuthenticationInvokesFailureHandler() throws Exception {
|
327 | 362 | // Setup our HTTP request
|
|
0 commit comments