|
22 | 22 | import jakarta.servlet.Filter;
|
23 | 23 | import jakarta.servlet.http.HttpServletRequest;
|
24 | 24 | import jakarta.servlet.http.HttpServletResponse;
|
| 25 | + |
25 | 26 | import org.eclipse.jetty.http.HttpStatus;
|
26 | 27 | import org.junit.jupiter.api.Test;
|
27 | 28 | import org.junit.jupiter.api.extension.ExtendWith;
|
@@ -336,6 +337,43 @@ public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerWithRawTokenT
|
336 | 337 | // @formatter:on
|
337 | 338 | }
|
338 | 339 |
|
| 340 | + @Test |
| 341 | + public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerThenCsrfAuthenticationStrategyUses() |
| 342 | + throws Exception { |
| 343 | + this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers")) |
| 344 | + .autowire(); |
| 345 | + // @formatter:off |
| 346 | + MvcResult mvcResult1 = this.mvc.perform(get("/csrf")) |
| 347 | + .andExpect(status().isOk()) |
| 348 | + .andReturn(); |
| 349 | + // @formatter:on |
| 350 | + MockHttpServletRequest request1 = mvcResult1.getRequest(); |
| 351 | + MockHttpSession session = (MockHttpSession) request1.getSession(); |
| 352 | + CsrfTokenRepository repository = WebTestUtils.getCsrfTokenRepository(request1); |
| 353 | + // @formatter:off |
| 354 | + MockHttpServletRequestBuilder login = post("/login") |
| 355 | + .param("username", "user") |
| 356 | + .param("password", "password") |
| 357 | + .session(session) |
| 358 | + .with(csrf()); |
| 359 | + this.mvc.perform(login) |
| 360 | + .andExpect(status().is3xxRedirection()) |
| 361 | + .andExpect(redirectedUrl("/")); |
| 362 | + // @formatter:on |
| 363 | + assertThat(repository.loadToken(request1)).isNull(); |
| 364 | + // @formatter:off |
| 365 | + MvcResult mvcResult2 = this.mvc.perform(get("/csrf").session(session)) |
| 366 | + .andExpect(status().isOk()) |
| 367 | + .andReturn(); |
| 368 | + // @formatter:on |
| 369 | + MockHttpServletRequest request2 = mvcResult2.getRequest(); |
| 370 | + CsrfToken csrfToken = repository.loadToken(request2); |
| 371 | + CsrfToken csrfTokenAttribute = (CsrfToken) request2.getAttribute(CsrfToken.class.getName()); |
| 372 | + assertThat(csrfTokenAttribute).isNotNull(); |
| 373 | + assertThat(csrfTokenAttribute.getToken()).isNotBlank(); |
| 374 | + assertThat(csrfTokenAttribute.getToken()).isNotEqualTo(csrfToken.getToken()); |
| 375 | + } |
| 376 | + |
339 | 377 | @Test
|
340 | 378 | public void postWhenHasCsrfTokenButSessionExpiresThenRequestIsCancelledAfterSuccessfulAuthentication()
|
341 | 379 | throws Exception {
|
|
0 commit comments