|
15 | 15 | */
|
16 | 16 | package org.springframework.security.web.authentication;
|
17 | 17 |
|
18 |
| -import static org.assertj.core.api.Assertions.assertThat; |
19 |
| -import static org.mockito.ArgumentMatchers.any; |
20 |
| -import static org.mockito.ArgumentMatchers.eq; |
21 |
| -import static org.mockito.Mockito.mock; |
22 |
| -import static org.mockito.Mockito.verify; |
23 |
| -import static org.mockito.Mockito.verifyZeroInteractions; |
24 |
| -import static org.mockito.Mockito.when; |
25 |
| - |
26 | 18 | import javax.servlet.FilterChain;
|
27 | 19 | import javax.servlet.ServletException;
|
28 | 20 | import javax.servlet.ServletRequest;
|
|
35 | 27 | import org.junit.runner.RunWith;
|
36 | 28 | import org.mockito.Mock;
|
37 | 29 | import org.mockito.junit.MockitoJUnitRunner;
|
| 30 | + |
38 | 31 | import org.springframework.http.HttpStatus;
|
| 32 | +import org.springframework.mock.web.MockFilterChain; |
39 | 33 | import org.springframework.mock.web.MockHttpServletRequest;
|
40 | 34 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 35 | +import org.springframework.mock.web.MockHttpSession; |
41 | 36 | import org.springframework.security.authentication.AuthenticationManager;
|
42 | 37 | import org.springframework.security.authentication.AuthenticationManagerResolver;
|
43 | 38 | import org.springframework.security.authentication.BadCredentialsException;
|
|
46 | 41 | import org.springframework.security.core.context.SecurityContextHolder;
|
47 | 42 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
48 | 43 |
|
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | +import static org.mockito.ArgumentMatchers.any; |
| 46 | +import static org.mockito.ArgumentMatchers.eq; |
| 47 | +import static org.mockito.Mockito.mock; |
| 48 | +import static org.mockito.Mockito.verify; |
| 49 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 50 | +import static org.mockito.Mockito.when; |
| 51 | + |
49 | 52 | /**
|
50 | 53 | * @author Sergey Bespalov
|
51 | 54 | * @since 5.2.0
|
@@ -246,4 +249,24 @@ public void filterWhenNotMatchAndConvertAndAuthenticationSuccessThenContinues()
|
246 | 249 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
|
247 | 250 | }
|
248 | 251 |
|
| 252 | + // gh-7446 |
| 253 | + @Test |
| 254 | + public void filterWhenSuccessfulAuthenticationThenSessionIdChanges() throws Exception { |
| 255 | + Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE_USER"); |
| 256 | + when(this.authenticationConverter.convert(any())).thenReturn(authentication); |
| 257 | + when(this.authenticationManager.authenticate(any())).thenReturn(authentication); |
| 258 | + |
| 259 | + MockHttpSession session = new MockHttpSession(); |
| 260 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
| 261 | + request.setSession(session); |
| 262 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 263 | + FilterChain chain = new MockFilterChain(); |
| 264 | + |
| 265 | + String sessionId = session.getId(); |
| 266 | + AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManager, this.authenticationConverter); |
| 267 | + filter.doFilter(request, response, chain); |
| 268 | + |
| 269 | + assertThat(session.getId()).isNotEqualTo(sessionId); |
| 270 | + } |
| 271 | + |
249 | 272 | }
|
0 commit comments