Skip to content

Commit c84c438

Browse files
committedMar 28, 2025
Apply request-handler-ref to CsrfAuthenticationStrategy
Closes gh-16801
1 parent 1e7db09 commit c84c438

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

‎config/src/main/java/org/springframework/security/config/http/CsrfBeanDefinitionParser.java

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ BeanDefinition getCsrfAuthenticationStrategy() {
183183
BeanDefinitionBuilder csrfAuthenticationStrategy = BeanDefinitionBuilder
184184
.rootBeanDefinition(CsrfAuthenticationStrategy.class);
185185
csrfAuthenticationStrategy.addConstructorArgReference(this.csrfRepositoryRef);
186+
if (StringUtils.hasText(this.requestHandlerRef)) {
187+
csrfAuthenticationStrategy.addPropertyReference("requestHandler", this.requestHandlerRef);
188+
}
186189
return csrfAuthenticationStrategy.getBeanDefinition();
187190
}
188191

‎config/src/test/java/org/springframework/security/config/http/CsrfConfigTests.java

+38
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.servlet.Filter;
2323
import jakarta.servlet.http.HttpServletRequest;
2424
import jakarta.servlet.http.HttpServletResponse;
25+
2526
import org.eclipse.jetty.http.HttpStatus;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.api.extension.ExtendWith;
@@ -336,6 +337,43 @@ public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerWithRawTokenT
336337
// @formatter:on
337338
}
338339

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+
339377
@Test
340378
public void postWhenHasCsrfTokenButSessionExpiresThenRequestIsCancelledAfterSuccessfulAuthentication()
341379
throws Exception {

0 commit comments

Comments
 (0)
Please sign in to comment.