|
|
|
@ -41,6 +41,7 @@ import org.springframework.security.web.FilterChainProxy; |
|
|
|
import org.springframework.security.web.access.AccessDeniedHandler; |
|
|
|
import org.springframework.security.web.access.AccessDeniedHandler; |
|
|
|
import org.springframework.security.web.csrf.CsrfFilter; |
|
|
|
import org.springframework.security.web.csrf.CsrfFilter; |
|
|
|
import org.springframework.security.web.csrf.CsrfToken; |
|
|
|
import org.springframework.security.web.csrf.CsrfToken; |
|
|
|
|
|
|
|
import org.springframework.security.web.csrf.CsrfTokenRepository; |
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher; |
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher; |
|
|
|
import org.springframework.stereotype.Controller; |
|
|
|
import org.springframework.stereotype.Controller; |
|
|
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
|
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
|
|
@ -301,7 +302,7 @@ public class CsrfConfigTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void postWhenUsingCsrfAndXorCsrfTokenRequestProcessorThenOk() throws Exception { |
|
|
|
public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerThenOk() throws Exception { |
|
|
|
this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers")) |
|
|
|
this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers")) |
|
|
|
.autowire(); |
|
|
|
.autowire(); |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
@ -309,25 +310,27 @@ public class CsrfConfigTests { |
|
|
|
.andExpect(status().isOk()) |
|
|
|
.andExpect(status().isOk()) |
|
|
|
.andReturn(); |
|
|
|
.andReturn(); |
|
|
|
MockHttpSession session = (MockHttpSession) mvcResult.getRequest().getSession(); |
|
|
|
MockHttpSession session = (MockHttpSession) mvcResult.getRequest().getSession(); |
|
|
|
CsrfToken csrfToken = (CsrfToken) mvcResult.getRequest().getAttribute("_csrf"); |
|
|
|
|
|
|
|
MockHttpServletRequestBuilder ok = post("/ok") |
|
|
|
MockHttpServletRequestBuilder ok = post("/ok") |
|
|
|
.header(csrfToken.getHeaderName(), csrfToken.getToken()) |
|
|
|
.with(csrf()) |
|
|
|
.session(session); |
|
|
|
.session(session); |
|
|
|
this.mvc.perform(ok).andExpect(status().isOk()); |
|
|
|
this.mvc.perform(ok).andExpect(status().isOk()); |
|
|
|
// @formatter:on
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void postWhenUsingCsrfAndXorCsrfTokenRequestProcessorWithRawTokenThenForbidden() throws Exception { |
|
|
|
public void postWhenUsingCsrfAndXorCsrfTokenRequestAttributeHandlerWithRawTokenThenForbidden() throws Exception { |
|
|
|
this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers")) |
|
|
|
this.spring.configLocations(this.xml("WithXorCsrfTokenRequestAttributeHandler"), this.xml("shared-controllers")) |
|
|
|
.autowire(); |
|
|
|
.autowire(); |
|
|
|
// @formatter:off
|
|
|
|
// @formatter:off
|
|
|
|
MvcResult mvcResult = this.mvc.perform(get("/ok")) |
|
|
|
MvcResult mvcResult = this.mvc.perform(get("/csrf")) |
|
|
|
.andExpect(status().isOk()) |
|
|
|
.andExpect(status().isOk()) |
|
|
|
.andReturn(); |
|
|
|
.andReturn(); |
|
|
|
MockHttpSession session = (MockHttpSession) mvcResult.getRequest().getSession(); |
|
|
|
MockHttpServletRequest request = mvcResult.getRequest(); |
|
|
|
|
|
|
|
MockHttpSession session = (MockHttpSession) request.getSession(); |
|
|
|
|
|
|
|
CsrfTokenRepository repository = WebTestUtils.getCsrfTokenRepository(request); |
|
|
|
|
|
|
|
CsrfToken csrfToken = repository.loadToken(request); |
|
|
|
MockHttpServletRequestBuilder ok = post("/ok") |
|
|
|
MockHttpServletRequestBuilder ok = post("/ok") |
|
|
|
.with(csrf()) |
|
|
|
.header(csrfToken.getHeaderName(), csrfToken.getToken()) |
|
|
|
.session(session); |
|
|
|
.session(session); |
|
|
|
this.mvc.perform(ok).andExpect(status().isForbidden()); |
|
|
|
this.mvc.perform(ok).andExpect(status().isForbidden()); |
|
|
|
// @formatter:on
|
|
|
|
// @formatter:on
|
|
|
|
@ -594,7 +597,7 @@ public class CsrfConfigTests { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
MockHttpServletRequest request = result.getRequest(); |
|
|
|
MockHttpServletRequest request = result.getRequest(); |
|
|
|
CsrfToken token = WebTestUtils.getCsrfTokenRepository(request).loadToken(request); |
|
|
|
CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); |
|
|
|
assertThat(token).isNotNull(); |
|
|
|
assertThat(token).isNotNull(); |
|
|
|
assertThat(token.getToken()).isEqualTo(this.token.apply(result)); |
|
|
|
assertThat(token.getToken()).isEqualTo(this.token.apply(result)); |
|
|
|
} |
|
|
|
} |
|
|
|
|