Skip to content

Commit 26c63ae

Browse files
committed
Merge branch '6.3.x' into 6.4.x
Closes gh-16844
2 parents 1ad4323 + b7df861 commit 26c63ae

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-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

+37
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,43 @@ public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerWithRawTokenT
336336
// @formatter:on
337337
}
338338

339+
@Test
340+
public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerThenCsrfAuthenticationStrategyUses()
341+
throws Exception {
342+
this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers"))
343+
.autowire();
344+
// @formatter:off
345+
MvcResult mvcResult1 = this.mvc.perform(get("/csrf"))
346+
.andExpect(status().isOk())
347+
.andReturn();
348+
// @formatter:on
349+
MockHttpServletRequest request1 = mvcResult1.getRequest();
350+
MockHttpSession session = (MockHttpSession) request1.getSession();
351+
CsrfTokenRepository repository = WebTestUtils.getCsrfTokenRepository(request1);
352+
// @formatter:off
353+
MockHttpServletRequestBuilder login = post("/login")
354+
.param("username", "user")
355+
.param("password", "password")
356+
.session(session)
357+
.with(csrf());
358+
this.mvc.perform(login)
359+
.andExpect(status().is3xxRedirection())
360+
.andExpect(redirectedUrl("/"));
361+
// @formatter:on
362+
assertThat(repository.loadToken(request1)).isNull();
363+
// @formatter:off
364+
MvcResult mvcResult2 = this.mvc.perform(get("/csrf").session(session))
365+
.andExpect(status().isOk())
366+
.andReturn();
367+
// @formatter:on
368+
MockHttpServletRequest request2 = mvcResult2.getRequest();
369+
CsrfToken csrfToken = repository.loadToken(request2);
370+
CsrfToken csrfTokenAttribute = (CsrfToken) request2.getAttribute(CsrfToken.class.getName());
371+
assertThat(csrfTokenAttribute).isNotNull();
372+
assertThat(csrfTokenAttribute.getToken()).isNotBlank();
373+
assertThat(csrfTokenAttribute.getToken()).isNotEqualTo(csrfToken.getToken());
374+
}
375+
339376
@Test
340377
public void postWhenHasCsrfTokenButSessionExpiresThenRequestIsCancelledAfterSuccessfulAuthentication()
341378
throws Exception {

0 commit comments

Comments
 (0)