Skip to content

Commit a63a0e3

Browse files
committed
Add reactive CSRF samples to docs
Issue gh-8172
1 parent da05543 commit a63a0e3

File tree

1 file changed

+81
-7
lines changed
  • docs/manual/src/docs/asciidoc/_includes/reactive/exploits

1 file changed

+81
-7
lines changed

docs/manual/src/docs/asciidoc/_includes/reactive/exploits/csrf.adoc

+81-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ These defaults come from https://docs.angularjs.org/api/ng/service/$http#cross-s
3434

3535
You can configure `CookieCsrfTokenRepository` in Java Configuration using:
3636

37-
.Store CSRF Token in a Cookie with Java Configuration
37+
.Store CSRF Token in a Cookie
3838
====
39-
[source,java]
39+
.Java
40+
[source,java,role="primary"]
4041
-----
4142
@Bean
4243
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -46,6 +47,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
4647
return http.build();
4748
}
4849
-----
50+
51+
.Kotlin
52+
[source,kotlin,role="secondary"]
53+
-----
54+
@Bean
55+
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
56+
return http {
57+
// ...
58+
csrf {
59+
csrfTokenRepository = CookieServerCsrfTokenRepository.withHttpOnlyFalse()
60+
}
61+
}
62+
}
63+
-----
4964
====
5065

5166
[NOTE]
@@ -62,9 +77,10 @@ However, it is simple to disable CSRF protection if it <<csrf-when,makes sense f
6277

6378
The Java configuration below will disable CSRF protection.
6479

65-
.Disable CSRF Java Configuration
80+
.Disable CSRF Configuration
6681
====
67-
[source,java]
82+
.Java
83+
[source,java,role="primary"]
6884
----
6985
@Bean
7086
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -74,6 +90,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
7490
return http.build();
7591
}
7692
----
93+
94+
.Kotlin
95+
[source,kotlin,role="secondary"]
96+
-----
97+
@Bean
98+
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
99+
return http {
100+
// ...
101+
csrf {
102+
disable()
103+
}
104+
}
105+
}
106+
-----
77107
====
78108

79109
[[webflux-csrf-include]]
@@ -91,7 +121,8 @@ For example, the following code will place the `CsrfToken` on the default attrib
91121

92122
.`CsrfToken` as `@ModelAttribute`
93123
====
94-
[source,java]
124+
.Java
125+
[source,java,role="primary"]
95126
----
96127
@ControllerAdvice
97128
public class SecurityControllerAdvice {
@@ -103,6 +134,21 @@ public class SecurityControllerAdvice {
103134
}
104135
}
105136
----
137+
138+
.Kotlin
139+
[source,kotlin,role="secondary"]
140+
----
141+
@ControllerAdvice
142+
class SecurityControllerAdvice {
143+
@ModelAttribute
144+
fun csrfToken(exchange: ServerWebExchange): Mono<CsrfToken> {
145+
val csrfToken: Mono<CsrfToken>? = exchange.getAttribute(CsrfToken::class.java.name)
146+
return csrfToken!!.doOnSuccess { token ->
147+
exchange.attributes[CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME] = token
148+
}
149+
}
150+
}
151+
----
106152
====
107153

108154
Fortunately, Thymeleaf provides <<webflux-csrf-include-form-auto,integration>> that works without any additional work.
@@ -253,7 +299,8 @@ For example, the following Java Configuration will perform logout with the URL `
253299

254300
.Log out with HTTP GET
255301
====
256-
[source,java]
302+
.Java
303+
[source,java,role="primary"]
257304
----
258305
@Bean
259306
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -262,7 +309,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
262309
.logout(logout -> logout.requiresLogout(new PathPatternParserServerWebExchangeMatcher("/logout")))
263310
return http.build();
264311
}
312+
----
265313
314+
.Kotlin
315+
[source,kotlin,role="secondary"]
316+
----
317+
@Bean
318+
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
319+
return http {
320+
// ...
321+
logout {
322+
requiresLogout = PathPatternParserServerWebExchangeMatcher("/logout")
323+
}
324+
}
325+
}
266326
----
267327
====
268328

@@ -301,7 +361,8 @@ In a WebFlux application, this can be configured with the following configuratio
301361

302362
.Enable obtaining CSRF token from multipart/form-data
303363
====
304-
[source,java]
364+
.Java
365+
[source,java,role="primary"]
305366
----
306367
@Bean
307368
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -310,7 +371,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
310371
.csrf(csrf -> csrf.tokenFromMultipartDataEnabled(true))
311372
return http.build();
312373
}
374+
----
313375
376+
.Kotlin
377+
[source,kotlin,role="secondary"]
378+
----
379+
@Bean
380+
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
381+
return http {
382+
// ...
383+
csrf {
384+
tokenFromMultipartDataEnabled = true
385+
}
386+
}
387+
}
314388
----
315389
====
316390

0 commit comments

Comments
 (0)