I tried to adapt the documentation for reactive applications (spring-cloud-gateway used as BFF) and have configured it as an OAuth2 client with:
http.csrf().csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(new XorServerCsrfTokenRequestAttributeHandler()::handle);
but I still had a "An expected CSRF token cannot be found" error, and actually, I couldn't find the XSRF-TOKEN
cookie is my browser debugging tools.
I then defined such a WebFilter
:
@Bean
WebFilter csrfCookieWebFilter() {
return (exchange, chain) -> {
Mono<CsrfToken> csrfToken = exchange.getAttributeOrDefault(CsrfToken.class.getName(), Mono.empty());
return csrfToken.doOnSuccess(token -> {
}).then(chain.filter(exchange));
};
}
I now have a XSRF-TOKEN cookie, but also an "Invalid CSRF Token" error.
So, what is the CookieServerCsrfTokenRepository
exactly and why couldn't I find the CSRF token cookie with it on spring-cloud-gateway?
How should I configure my spring-cloud-gateway to allow PUT requests to the /logout
endpoint from an Angular application?
Edit
As a very similar question was asked more than 1.5 month ago but is still unanswered: "Angular app served behind Spring Cloud Gateway cannot send POST requests to backend because of invalid CSRF token", I opened a ticket for spring-security: https://github.com/spring-projects/spring-security/issues/12871
The other question is missing the part in the doc I linked, but still, it should produce a CSRF cookie (even if the value then fails to be validated because of the new BREACH proof handler).