Skip to content

Commit 72acc29

Browse files
committed
Add servlet CSRF Kotlin samples
Issue gh-8172
1 parent a5b97bb commit 72acc29

File tree

1 file changed

+69
-6
lines changed
  • docs/manual/src/docs/asciidoc/_includes/servlet/exploits

1 file changed

+69
-6
lines changed

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

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ If you do not need the ability to read the cookie with JavaScript directly, it i
5959

6060
You can configure `CookieCsrfTokenRepository` in Java Configuration using:
6161

62-
.Store CSRF Token in a Cookie with Java Configuration
62+
.Store CSRF Token in a Cookie
6363
====
64-
[source,java]
64+
.Java
65+
[source,java,role="primary"]
6566
----
6667
@EnableWebSecurity
6768
public class WebSecurityConfig extends
@@ -76,6 +77,22 @@ public class WebSecurityConfig extends
7677
}
7778
}
7879
----
80+
81+
.Kotlin
82+
[source,kotlin,role="secondary"]
83+
----
84+
@EnableWebSecurity
85+
class SecurityConfig : WebSecurityConfigurerAdapter() {
86+
87+
override fun configure(http: HttpSecurity) {
88+
http {
89+
csrf {
90+
csrfTokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse()
91+
}
92+
}
93+
}
94+
}
95+
----
7996
====
8097

8198
[NOTE]
@@ -106,9 +123,10 @@ The XML configuration below will disable CSRF protection.
106123

107124
The Java configuration below will disable CSRF protection.
108125

109-
.Disable CSRF Java Configuration
126+
.Disable CSRF
110127
====
111-
[source,java]
128+
.Java
129+
[source,java,role="primary"]
112130
----
113131
@Configuration
114132
@EnableWebSecurity
@@ -122,6 +140,23 @@ public class WebSecurityConfig extends
122140
}
123141
}
124142
----
143+
144+
.Kotlin
145+
[source,kotlin,role="secondary"]
146+
----
147+
@Configuration
148+
@EnableWebSecurity
149+
class SecurityConfig : WebSecurityConfigurerAdapter() {
150+
151+
override fun configure(http: HttpSecurity) {
152+
http {
153+
csrf {
154+
disable()
155+
}
156+
}
157+
}
158+
}
159+
----
125160
====
126161

127162
[[servlet-csrf-include]]
@@ -291,7 +326,8 @@ For example, the following Java Configuration will perform logout with the URL `
291326

292327
.Log out with HTTP GET
293328
====
294-
[source,java]
329+
.Java
330+
[source,java,role="primary"]
295331
----
296332
@EnableWebSecurity
297333
public class WebSecurityConfig extends
@@ -306,6 +342,22 @@ public class WebSecurityConfig extends
306342
}
307343
}
308344
----
345+
346+
.Kotlin
347+
[source,kotlin,role="secondary"]
348+
----
349+
@EnableWebSecurity
350+
class SecurityConfig : WebSecurityConfigurerAdapter() {
351+
352+
override fun configure(http: HttpSecurity) {
353+
http {
354+
logout {
355+
logoutRequestMatcher = AntPathRequestMatcher("/logout")
356+
}
357+
}
358+
}
359+
}
360+
----
309361
====
310362

311363

@@ -354,7 +406,8 @@ To ensure `MultipartFilter` is specified before the Spring Security filter with
354406

355407
.Initializer MultipartFilter
356408
====
357-
[source,java]
409+
.Java
410+
[source,java,role="primary"]
358411
----
359412
public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
360413
@@ -364,6 +417,16 @@ public class SecurityApplicationInitializer extends AbstractSecurityWebApplicati
364417
}
365418
}
366419
----
420+
421+
.Kotlin
422+
[source,kotlin,role="secondary"]
423+
----
424+
class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer() {
425+
override fun beforeSpringSecurityFilterChain(servletContext: ServletContext?) {
426+
insertFilters(servletContext, MultipartFilter())
427+
}
428+
}
429+
----
367430
====
368431

369432
To ensure `MultipartFilter` is specified before the Spring Security filter with XML configuration, users can ensure the <filter-mapping> element of the `MultipartFilter` is placed before the springSecurityFilterChain within the web.xml as shown below:

0 commit comments

Comments
 (0)