Skip to content

Commit 4112adf

Browse files
committed
Document Configure Default CsrfTOken BREACH Protection
Closes gh-12107
1 parent 96d7c78 commit 4112adf

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

docs/modules/ROOT/pages/migration.adoc

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
7373

7474
If this breaks your application, then you can explicitly opt into the 5.8 defaults using the following configuration:
7575

76-
.Defer Loading `CsrfToken`
76+
.Explicit Configure `CsrfToken` with 5.8 Defaults
7777
====
7878
.Java
7979
[source,java,role="primary"]
@@ -125,6 +125,59 @@ open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
125125
----
126126
====
127127

128+
=== CSRF BREACH Protection
129+
130+
If the steps for <<Defer Loading CsrfToken>> work for you, then you can also opt into Spring Security 6's default support for BREACH protection of the `CsrfToken` using the following configuration:
131+
132+
.`CsrfToken` BREACH Protection
133+
====
134+
.Java
135+
[source,java,role="primary"]
136+
----
137+
@Bean
138+
DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
139+
XorCsrfTokenRequestAttributeHandler requestHandler = new XorCsrfTokenRequestAttributeHandler();
140+
// set the name of the attribute the CsrfToken will be populated on
141+
requestHandler.setCsrfRequestAttributeName("_csrf");
142+
http
143+
// ...
144+
.csrf((csrf) -> csrf
145+
.csrfTokenRequestHandler(requestHandler)
146+
);
147+
return http.build();
148+
}
149+
----
150+
151+
.Kotlin
152+
[source,kotlin,role="secondary"]
153+
----
154+
@Bean
155+
open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
156+
val requestHandler = XorCsrfTokenRequestAttributeHandler()
157+
// set the name of the attribute the CsrfToken will be populated on
158+
requestHandler.setCsrfRequestAttributeName("_csrf")
159+
http {
160+
csrf {
161+
csrfTokenRequestHandler = requestHandler
162+
}
163+
}
164+
return http.build()
165+
}
166+
----
167+
168+
.XML
169+
[source,xml,role="secondary"]
170+
----
171+
<http>
172+
<!-- ... -->
173+
<csrf request-handler-ref="requestHandler"/>
174+
</http>
175+
<b:bean id="requestHandler"
176+
class="org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler"
177+
p:csrfRequestAttributeName="_csrf"/>
178+
----
179+
====
180+
128181
=== Explicit Save SecurityContextRepository
129182

130183
In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].

0 commit comments

Comments
 (0)