Merge branch '5.8.x'
# Conflicts: # docs/modules/ROOT/pages/servlet/exploits/csrf.adoc
This commit is contained in:
@@ -164,13 +164,76 @@ class SecurityConfig {
|
||||
----
|
||||
====
|
||||
|
||||
[[servlet-csrf-configure-request-handler]]
|
||||
==== Configure CsrfTokenRequestHandler
|
||||
|
||||
Spring Security's https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfFilter.html[`CsrfFilter`] exposes a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfToken.html[`CsrfToken`] as an `HttpServletRequest` attribute named `_csrf` with the help of a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfTokenRequestHandler.html[CsrfTokenRequestHandler].
|
||||
The default implementation is `CsrfTokenRequestAttributeHandler`.
|
||||
|
||||
An alternate implementation `XorCsrfTokenRequestAttributeHandler` is available to provide protection for BREACH (see https://github.com/spring-projects/spring-security/issues/4001[gh-4001]).
|
||||
|
||||
You can configure `XorCsrfTokenRequestAttributeHandler` in XML using the following:
|
||||
|
||||
.Configure BREACH protection XML Configuration
|
||||
====
|
||||
[source,xml]
|
||||
----
|
||||
<http>
|
||||
<!-- ... -->
|
||||
<csrf request-handler-ref="requestHandler"/>
|
||||
</http>
|
||||
<b:bean id="requestHandler"
|
||||
class="org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler"/>
|
||||
----
|
||||
====
|
||||
|
||||
You can configure `XorCsrfTokenRequestAttributeHandler` in Java Configuration using the following:
|
||||
|
||||
.Configure BREACH protection
|
||||
====
|
||||
.Java
|
||||
[source,java,role="primary"]
|
||||
----
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf(csrf -> csrf
|
||||
.csrfTokenRequestHandler(new XorCsrfTokenRequestAttributeHandler())
|
||||
);
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
.Kotlin
|
||||
[source,kotlin,role="secondary"]
|
||||
----
|
||||
@EnableWebSecurity
|
||||
class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
|
||||
http {
|
||||
csrf {
|
||||
csrfTokenRequestHandler = XorCsrfTokenRequestAttributeHandler()
|
||||
}
|
||||
}
|
||||
return http.build()
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
[[servlet-csrf-include]]
|
||||
=== Include the CSRF Token
|
||||
|
||||
For the xref:features/exploits/csrf.adoc#csrf-protection-stp[synchronizer token pattern] to protect against CSRF attacks, we must include the actual CSRF token in the HTTP request.
|
||||
This must be included in a part of the request (a form parameter, an HTTP header, or other part) that is not automatically included in the HTTP request by the browser.
|
||||
|
||||
Spring Security's https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfFilter.html[`CsrfFilter`] exposes a https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/csrf/CsrfToken.html[`CsrfToken`] as an `HttpServletRequest` attribute named `_csrf`.
|
||||
<<servlet-csrf-configure-request-handler,We've seen>> that the `CsrfToken` is exposed as a request attribute.
|
||||
This means that any view technology can access the `CsrfToken` to expose the expected token as either a <<servlet-csrf-include-form-attr,form>> or <<servlet-csrf-include-ajax-meta-attr,meta tag>>.
|
||||
Fortunately, there are integrations listed later in this chapter that make including the token in <<servlet-csrf-include-form,form>> and <<servlet-csrf-include-ajax,ajax>> requests even easier.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user