|
@@ -9,8 +9,10 @@ You can specify the default configuration explicitly using the following:
|
|
|
|
|
|
[[csrf-configuration]]
|
|
|
.Configure CSRF Protection
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -27,7 +29,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -47,7 +50,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -55,7 +59,7 @@ class SecurityConfig {
|
|
|
<csrf/>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
To learn more about CSRF protection for your application, consider the following use cases:
|
|
|
|
|
@@ -132,8 +136,10 @@ You can specify the default configuration explicitly using the following configu
|
|
|
|
|
|
[[csrf-token-repository-httpsession-configuration]]
|
|
|
.Configure `HttpSessionCsrfTokenRepository`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -152,7 +158,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -174,7 +181,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -184,7 +192,7 @@ class SecurityConfig {
|
|
|
<b:bean id="tokenRepository"
|
|
|
class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-token-repository-cookie]]
|
|
|
=== Using the `CookieCsrfTokenRepository`
|
|
@@ -203,8 +211,10 @@ You can configure the `CookieCsrfTokenRepository` using the following configurat
|
|
|
|
|
|
[[csrf-token-repository-cookie-configuration]]
|
|
|
.Configure `CookieCsrfTokenRepository`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -223,7 +233,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -245,7 +256,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -256,7 +268,7 @@ class SecurityConfig {
|
|
|
class="org.springframework.security.web.csrf.CookieCsrfTokenRepository"
|
|
|
p:cookieHttpOnly="false"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
@@ -274,8 +286,10 @@ Once you've implemented the `CsrfTokenRepository` interface, you can configure S
|
|
|
|
|
|
[[csrf-token-repository-custom-configuration]]
|
|
|
.Configure Custom `CsrfTokenRepository`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -294,7 +308,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -316,7 +331,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -326,7 +342,7 @@ class SecurityConfig {
|
|
|
<b:bean id="tokenRepository"
|
|
|
class="example.CustomCsrfTokenRepository"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-token-request-handler]]
|
|
|
== Handling the `CsrfToken`
|
|
@@ -362,8 +378,10 @@ You can specify the default configuration explicitly using the following configu
|
|
|
|
|
|
[[csrf-token-request-handler-breach-configuration]]
|
|
|
.Configure BREACH protection
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -382,7 +400,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -404,7 +423,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -414,7 +434,7 @@ class SecurityConfig {
|
|
|
<b:bean id="requestHandler"
|
|
|
class="org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-token-request-handler-plain]]
|
|
|
=== Using the `CsrfTokenRequestAttributeHandler`
|
|
@@ -433,8 +453,10 @@ This implementation also resolves the token value from the request as either a r
|
|
|
The primary use of `CsrfTokenRequestAttributeHandler` is to opt-out of BREACH protection of the `CsrfToken`, which can be configured using the following configuration:
|
|
|
|
|
|
.Opt-out of BREACH protection
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -453,7 +475,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -475,7 +498,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -485,7 +509,7 @@ class SecurityConfig {
|
|
|
<b:bean id="requestHandler"
|
|
|
class="org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-token-request-handler-custom]]
|
|
|
=== Customizing the `CsrfTokenRequestHandler`
|
|
@@ -503,8 +527,10 @@ Once you've implemented the `CsrfTokenRequestHandler` interface, you can configu
|
|
|
|
|
|
[[csrf-token-request-handler-custom-configuration]]
|
|
|
.Configure Custom `CsrfTokenRequestHandler`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -523,7 +549,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -545,7 +572,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -555,7 +583,7 @@ class SecurityConfig {
|
|
|
<b:bean id="requestHandler"
|
|
|
class="example.CustomCsrfTokenRequestHandler"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[deferred-csrf-token]]
|
|
|
== Deferred Loading of the `CsrfToken`
|
|
@@ -575,8 +603,10 @@ In the event that you want to opt-out of deferred tokens and cause the `CsrfToke
|
|
|
|
|
|
[[deferred-csrf-token-opt-out-configuration]]
|
|
|
.Opt-out of Deferred CSRF Tokens
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -598,7 +628,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -623,7 +654,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -637,7 +669,7 @@ class SecurityConfig {
|
|
|
</b:property>
|
|
|
</b:bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
@@ -665,14 +697,12 @@ To submit an HTML form, the CSRF token must be included in the form as a hidden
|
|
|
For example, the rendered HTML might look like:
|
|
|
|
|
|
.CSRF Token in HTML Form
|
|
|
-====
|
|
|
[source,html]
|
|
|
----
|
|
|
<input type="hidden"
|
|
|
name="_csrf"
|
|
|
value="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
The following view technologies automatically include the actual CSRF token in a form that has an unsafe HTTP method, such as a POST:
|
|
|
|
|
@@ -685,7 +715,6 @@ If these options are not available, you can take advantage of the fact that the
|
|
|
The following example does this with a JSP:
|
|
|
|
|
|
.CSRF Token in HTML Form with Request Attribute
|
|
|
-====
|
|
|
[source,xml]
|
|
|
----
|
|
|
<c:url var="logoutUrl" value="/logout"/>
|
|
@@ -698,7 +727,6 @@ The following example does this with a JSP:
|
|
|
value="${_csrf.token}"/>
|
|
|
</form>
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
[[csrf-integration-javascript]]
|
|
|
=== JavaScript Applications
|
|
@@ -743,8 +771,10 @@ In order to easily integrate a single-page application with Spring Security, the
|
|
|
|
|
|
[[csrf-integration-javascript-spa-configuration]]
|
|
|
.Configure CSRF for Single-Page Application
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -811,7 +841,8 @@ final class CsrfCookieFilter extends OncePerRequestFilter {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -878,7 +909,8 @@ class CsrfCookieFilter : OncePerRequestFilter() {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -896,7 +928,7 @@ class CsrfCookieFilter : OncePerRequestFilter() {
|
|
|
<b:bean id="csrfCookieFilter"
|
|
|
class="example.CsrfCookieFilter"/>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
<1> Configure `CookieCsrfTokenRepository` with `HttpOnly` set to `false` so the cookie can be read by the JavaScript application.
|
|
|
<2> Configure a custom `CsrfTokenRequestHandler` that resolves the CSRF token based on whether it is an HTTP request header (`X-XSRF-TOKEN`) or request parameter (`_csrf`).
|
|
@@ -909,7 +941,6 @@ For multi-page applications where JavaScript is loaded on each page, an alternat
|
|
|
The HTML might look something like this:
|
|
|
|
|
|
.CSRF Token in HTML Meta Tag
|
|
|
-====
|
|
|
[source,html]
|
|
|
----
|
|
|
<html>
|
|
@@ -921,13 +952,11 @@ The HTML might look something like this:
|
|
|
<!-- ... -->
|
|
|
</html>
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
In order to include the CSRF token in the request, you can take advantage of the fact that the `CsrfToken` is exposed as an <<csrf-token-request-handler,`HttpServletRequest` attribute named `_csrf`>>.
|
|
|
The following example does this with a JSP:
|
|
|
|
|
|
.CSRF Token in HTML Meta Tag with Request Attribute
|
|
|
-====
|
|
|
[source,html]
|
|
|
----
|
|
|
<html>
|
|
@@ -940,13 +969,11 @@ The following example does this with a JSP:
|
|
|
<!-- ... -->
|
|
|
</html>
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
Once the meta tags contain the CSRF token, the JavaScript code can read the meta tags and include the CSRF token as a header.
|
|
|
If you use jQuery, you can do this with the following code:
|
|
|
|
|
|
.Include CSRF Token in AJAX Request
|
|
|
-====
|
|
|
[source,javascript]
|
|
|
----
|
|
|
$(function () {
|
|
@@ -957,7 +984,6 @@ $(function () {
|
|
|
});
|
|
|
});
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
[[csrf-integration-javascript-other]]
|
|
|
==== Other JavaScript Applications
|
|
@@ -969,8 +995,10 @@ The following is an example of `@ControllerAdvice` that applies to all controlle
|
|
|
|
|
|
[[controller-advice]]
|
|
|
.CSRF Token in HTTP Response Header
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@ControllerAdvice
|
|
@@ -984,7 +1012,8 @@ public class CsrfControllerAdvice {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@ControllerAdvice
|
|
@@ -997,7 +1026,7 @@ class CsrfControllerAdvice {
|
|
|
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
@@ -1038,8 +1067,10 @@ The following is an example of the `/csrf` endpoint that makes use of the xref:s
|
|
|
|
|
|
[[csrf-endpoint]]
|
|
|
.The `/csrf` endpoint
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@RestController
|
|
@@ -1053,7 +1084,8 @@ public class CsrfController {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@RestController
|
|
@@ -1066,7 +1098,7 @@ class CsrfController {
|
|
|
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
@@ -1091,8 +1123,10 @@ For example, you can configure a custom access denied page using the following c
|
|
|
|
|
|
[[csrf-access-denied-handler-configuration]]
|
|
|
.Configure `AccessDeniedHandler`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -1111,7 +1145,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -1133,7 +1168,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -1141,7 +1177,7 @@ class SecurityConfig {
|
|
|
<access-denied-handler error-page="/access-denied"/>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-testing]]
|
|
|
== CSRF Testing
|
|
@@ -1150,8 +1186,10 @@ You can use Spring Security's xref:servlet/test/mockmvc/setup.adoc[testing suppo
|
|
|
|
|
|
[[csrf-testing-example]]
|
|
|
.Test CSRF Protection
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
|
|
@@ -1194,7 +1232,8 @@ public class CsrfTests {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*
|
|
@@ -1236,7 +1275,7 @@ class CsrfTests {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[disable-csrf]]
|
|
|
== Disable CSRF Protection
|
|
@@ -1248,8 +1287,10 @@ You can also consider whether only certain endpoints do not require CSRF protect
|
|
|
|
|
|
[[disable-csrf-ignoring-configuration]]
|
|
|
.Ignoring Requests
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -1268,7 +1309,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -1290,7 +1332,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -1309,14 +1352,16 @@ class SecurityConfig {
|
|
|
</b:constructor-arg>
|
|
|
</b:bean>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
If you need to disable CSRF protection, you can do so using the following configuration:
|
|
|
|
|
|
[[disable-csrf-configuration]]
|
|
|
.Disable CSRF
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -1333,7 +1378,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -1355,7 +1401,8 @@ class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<http>
|
|
@@ -1363,7 +1410,7 @@ class SecurityConfig {
|
|
|
<csrf disabled="true"/>
|
|
|
</http>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[[csrf-considerations]]
|
|
|
== CSRF Considerations
|
|
@@ -1394,8 +1441,10 @@ However, remember that this is generally not recommended.
|
|
|
For example, the following logs out when the `/logout` URL is requested with any HTTP method:
|
|
|
|
|
|
.Log Out with Any HTTP Method
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
@Configuration
|
|
@@ -1414,7 +1463,8 @@ public class SecurityConfig {
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
import org.springframework.security.config.annotation.web.invoke
|
|
@@ -1435,7 +1485,7 @@ class SecurityConfig {
|
|
|
}
|
|
|
}
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
See the xref:servlet/authentication/logout.adoc[Logout] chapter for more information.
|
|
|
|
|
@@ -1479,8 +1529,10 @@ However, only authorized users can submit a file that is processed by your appli
|
|
|
In general, this is the recommended approach because the temporary file upload should have a negligible impact on most servers.
|
|
|
|
|
|
.Configure `MultipartFilter`
|
|
|
-====
|
|
|
-.Java
|
|
|
+[tabs]
|
|
|
+======
|
|
|
+Java::
|
|
|
++
|
|
|
[source,java,role="primary"]
|
|
|
----
|
|
|
public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
|
|
@@ -1492,7 +1544,8 @@ public class SecurityApplicationInitializer extends AbstractSecurityWebApplicati
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.Kotlin
|
|
|
+Kotlin::
|
|
|
++
|
|
|
[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer() {
|
|
@@ -1502,7 +1555,8 @@ class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-.XML
|
|
|
+XML::
|
|
|
++
|
|
|
[source,xml,role="secondary"]
|
|
|
----
|
|
|
<filter>
|
|
@@ -1522,7 +1576,7 @@ class SecurityApplicationInitializer : AbstractSecurityWebApplicationInitializer
|
|
|
<url-pattern>/*</url-pattern>
|
|
|
</filter-mapping>
|
|
|
----
|
|
|
-====
|
|
|
+======
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
@@ -1537,14 +1591,12 @@ Since the `CsrfToken` is exposed as an <<csrf-token-request-handler,`HttpServlet
|
|
|
The following example does this with a JSP:
|
|
|
|
|
|
.CSRF Token in Action
|
|
|
-====
|
|
|
[source,html]
|
|
|
----
|
|
|
<form method="post"
|
|
|
action="./upload?${_csrf.parameterName}=${_csrf.token}"
|
|
|
enctype="multipart/form-data">
|
|
|
----
|
|
|
-====
|
|
|
|
|
|
[[csrf-considerations-override-method]]
|
|
|
=== HiddenHttpMethodFilter
|