@@ -34,9 +34,10 @@ These defaults come from https://docs.angularjs.org/api/ng/service/$http#cross-s
34
34
35
35
You can configure `CookieCsrfTokenRepository` in Java Configuration using:
36
36
37
- .Store CSRF Token in a Cookie with Java Configuration
37
+ .Store CSRF Token in a Cookie
38
38
====
39
- [source,java]
39
+ .Java
40
+ [source,java,role="primary"]
40
41
-----
41
42
@Bean
42
43
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -46,6 +47,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
46
47
return http.build();
47
48
}
48
49
-----
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
+ -----
49
64
====
50
65
51
66
[NOTE]
@@ -62,9 +77,10 @@ However, it is simple to disable CSRF protection if it <<csrf-when,makes sense f
62
77
63
78
The Java configuration below will disable CSRF protection.
64
79
65
- .Disable CSRF Java Configuration
80
+ .Disable CSRF Configuration
66
81
====
67
- [source,java]
82
+ .Java
83
+ [source,java,role="primary"]
68
84
----
69
85
@Bean
70
86
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -74,6 +90,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
74
90
return http.build();
75
91
}
76
92
----
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
+ -----
77
107
====
78
108
79
109
[[webflux-csrf-include]]
@@ -91,7 +121,8 @@ For example, the following code will place the `CsrfToken` on the default attrib
91
121
92
122
.`CsrfToken` as `@ModelAttribute`
93
123
====
94
- [source,java]
124
+ .Java
125
+ [source,java,role="primary"]
95
126
----
96
127
@ControllerAdvice
97
128
public class SecurityControllerAdvice {
@@ -103,6 +134,21 @@ public class SecurityControllerAdvice {
103
134
}
104
135
}
105
136
----
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
+ ----
106
152
====
107
153
108
154
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 `
253
299
254
300
.Log out with HTTP GET
255
301
====
256
- [source,java]
302
+ .Java
303
+ [source,java,role="primary"]
257
304
----
258
305
@Bean
259
306
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -262,7 +309,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
262
309
.logout(logout -> logout.requiresLogout(new PathPatternParserServerWebExchangeMatcher("/logout")))
263
310
return http.build();
264
311
}
312
+ ----
265
313
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
+ }
266
326
----
267
327
====
268
328
@@ -301,7 +361,8 @@ In a WebFlux application, this can be configured with the following configuratio
301
361
302
362
.Enable obtaining CSRF token from multipart/form-data
303
363
====
304
- [source,java]
364
+ .Java
365
+ [source,java,role="primary"]
305
366
----
306
367
@Bean
307
368
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
@@ -310,7 +371,20 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
310
371
.csrf(csrf -> csrf.tokenFromMultipartDataEnabled(true))
311
372
return http.build();
312
373
}
374
+ ----
313
375
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
+ }
314
388
----
315
389
====
316
390
0 commit comments