Merge branch '5.8.x' into 6.0.x

Closes gh-13406
This commit is contained in:
Rob Winch
2023-06-18 21:33:58 -05:00
116 changed files with 4826 additions and 3206 deletions

View File

@@ -35,8 +35,10 @@ These defaults come from https://docs.angularjs.org/api/ng/service/$http#cross-s
You can configure `CookieServerCsrfTokenRepository` in Java Configuration:
.Store CSRF Token in a Cookie
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
-----
@Bean
@@ -48,7 +50,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
}
-----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
-----
@Bean
@@ -61,7 +64,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
-----
====
======
[NOTE]
====
@@ -78,8 +81,10 @@ However, you can disable CSRF protection if it xref:features/exploits/csrf.adoc#
The Java configuration below will disable CSRF protection.
.Disable CSRF Configuration
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -91,7 +96,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
-----
@Bean
@@ -104,7 +110,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
-----
====
======
[[webflux-csrf-configure-request-handler]]
==== Configure ServerCsrfTokenRequestHandler
@@ -117,8 +123,10 @@ As of 6.0, the default implementation is `XorServerCsrfTokenRequestAttributeHand
If you wish to disable BREACH protection of the `CsrfToken` and revert to the 5.8 default, you can configure `ServerCsrfTokenRequestAttributeHandler` using the following Java configuration:
.Disable BREACH protection
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
-----
@Bean
@@ -132,7 +140,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
}
-----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
-----
@Bean
@@ -145,7 +154,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
-----
====
======
[[webflux-csrf-include]]
=== Include the CSRF Token
@@ -161,8 +170,10 @@ If your view technology does not provide a simple way to subscribe to the `Mono<
The following example places the `CsrfToken` on the default attribute name (`_csrf`) used by Spring Security's <<webflux-csrf-include-form-auto,CsrfRequestDataValueProcessor>> to automatically include the CSRF token as a hidden input:
.`CsrfToken` as `@ModelAttribute`
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@ControllerAdvice
@@ -176,7 +187,8 @@ public class SecurityControllerAdvice {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@ControllerAdvice
@@ -190,7 +202,7 @@ class SecurityControllerAdvice {
}
}
----
====
======
Fortunately, Thymeleaf provides <<webflux-csrf-include-form-auto,integration>> that works without any additional work.
@@ -200,14 +212,12 @@ To post an HTML form, the CSRF token must be included in the form as a hidden in
The following example shows what the rendered HTML might look like:
.CSRF Token HTML
====
[source,html]
----
<input type="hidden"
name="_csrf"
value="4bfd1575-3ad1-4d21-96c7-4ef2d9f86721"/>
----
====
Next, we discuss various ways of including the CSRF token in a form as a hidden input.
@@ -227,7 +237,6 @@ If the <<webflux-csrf-include,other options>> for including the actual CSRF toke
The following Thymeleaf sample assumes that you <<webflux-csrf-include-subscribe,expose>> the `CsrfToken` on an attribute named `_csrf`:
.CSRF Token in Form with Request Attribute
====
[source,html]
----
<form th:action="@{/logout}"
@@ -239,7 +248,6 @@ The following Thymeleaf sample assumes that you <<webflux-csrf-include-subscribe
th:value="${_csrf.token}"/>
</form>
----
====
[[webflux-csrf-include-ajax]]
==== Ajax and JSON Requests
@@ -261,7 +269,6 @@ An alternative pattern to <<webflux-csrf-include-form-auto,exposing the CSRF in
The HTML might look something like this:
.CSRF meta tag HTML
====
[source,html]
----
<html>
@@ -272,13 +279,11 @@ The HTML might look something like this:
</head>
<!-- ... -->
----
====
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 could read the meta tags with the following code:
.AJAX send CSRF Token
====
[source,javascript]
----
$(function () {
@@ -289,13 +294,11 @@ $(function () {
});
});
----
====
The following sample assumes that you <<webflux-csrf-include-subscribe,expose>> the `CsrfToken` on an attribute named `_csrf`.
The following example does this with Thymeleaf:
.CSRF meta tag JSP
====
[source,html]
----
<html>
@@ -307,7 +310,6 @@ The following example does this with Thymeleaf:
</head>
<!-- ... -->
----
====
[[webflux-csrf-considerations]]
== CSRF Considerations
@@ -339,8 +341,10 @@ For example, the following Java Configuration logs out when the `/logout` URL is
// FIXME: This should be a link to log out documentation
.Log out with HTTP GET
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -352,7 +356,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -365,7 +370,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
----
====
======
[[webflux-considerations-csrf-timeouts]]
@@ -401,8 +406,10 @@ We have xref:features/exploits/csrf.adoc#csrf-considerations-multipart[already d
In a WebFlux application, you can do so with the following configuration:
.Enable obtaining CSRF token from multipart/form-data
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -414,7 +421,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -427,7 +435,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
----
====
======
[[webflux-csrf-considerations-multipart-url]]
==== Include CSRF Token in URL
@@ -437,14 +445,12 @@ Since the `CsrfToken` is exposed as an `ServerHttpRequest` <<webflux-csrf-includ
An example with Thymeleaf is shown below:
.CSRF Token in Action
====
[source,html]
----
<form method="post"
th:action="@{/upload(${_csrf.parameterName}=${_csrf.token})}"
enctype="multipart/form-data">
----
====
[[webflux-csrf-considerations-override-method]]
=== HiddenHttpMethodFilter

View File

@@ -16,8 +16,10 @@ For example, assume that you want the defaults but you wish to specify `SAMEORIG
You can do so with the following configuration:
.Customize Default Security Headers
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -33,7 +35,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -48,14 +51,16 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
If you do not want the defaults to be added and want explicit control over what should be used, you can disable the defaults:
.Disable HTTP Security Response Headers
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -67,7 +72,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -80,7 +86,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-cache-control]]
== Cache Control
@@ -96,8 +102,10 @@ You can find details on how to do so in the https://docs.spring.io/spring/docs/5
If necessary, you can also disable Spring Security's cache control HTTP response headers.
.Cache Control Disabled
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -111,7 +119,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -126,7 +135,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-content-type-options]]
@@ -135,8 +144,10 @@ By default, Spring Security includes xref:features/exploits/headers.adoc#headers
However, you can disable it:
.Content Type Options Disabled
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -150,7 +161,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -165,7 +177,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-hsts]]
== HTTP Strict Transport Security (HSTS)
@@ -174,8 +186,10 @@ However, you can customize the results explicitly.
For example, the following example explicitly provides HSTS:
.Strict Transport Security
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -193,7 +207,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -210,7 +225,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-frame-options]]
== X-Frame-Options
@@ -219,8 +234,10 @@ By default, Spring Security disables rendering within an iframe by using xref:fe
You can customize frame options to use the same origin:
.X-Frame-Options: SAMEORIGIN
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -236,7 +253,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -251,7 +269,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-xss-protection]]
== X-XSS-Protection
@@ -259,8 +277,10 @@ By default, Spring Security instructs browsers to disable the XSS Auditor by usi
You can disable the `X-XSS-Protection` header entirely:
.X-XSS-Protection Customization
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -274,7 +294,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -289,13 +310,15 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
You can also change the header value:
.X-XSS-Protection Explicit header value
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -309,7 +332,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -324,7 +348,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-csp]]
== Content Security Policy (CSP)
@@ -334,18 +358,18 @@ The web application author must declare the security policies to enforce and/or
For example, consider the following security policy:
.Content Security Policy Example
====
[source,http]
----
Content-Security-Policy: script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/
----
====
Given the preceding policy, you can enable the CSP header:
.Content Security Policy
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -361,7 +385,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -376,13 +401,15 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
To enable the CSP `report-only` header, provide the following configuration:
.Content Security Policy Report Only
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -399,7 +426,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -415,7 +443,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-referrer]]
== Referrer Policy
@@ -424,8 +452,10 @@ By default, Spring Security does not add xref:features/exploits/headers.adoc#hea
You can enable the Referrer Policy header using configuration as shown below:
.Referrer Policy Configuration
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -441,7 +471,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -456,7 +487,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-feature]]
@@ -466,18 +497,18 @@ By default, Spring Security does not add xref:features/exploits/headers.adoc#hea
Consider the following `Feature-Policy` header:
.Feature-Policy Example
====
[source]
----
Feature-Policy: geolocation 'self'
----
====
You can enable the preceding Feature Policy header:
.Feature-Policy Configuration
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -491,7 +522,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -504,7 +536,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-permissions]]
@@ -514,18 +546,18 @@ By default, Spring Security does not add xref:features/exploits/headers.adoc#hea
Consider the following `Permissions-Policy` header:
.Permissions-Policy Example
====
[source]
----
Permissions-Policy: geolocation=(self)
----
====
You can enable the preceding Permissions Policy header:
.Permissions-Policy Configuration
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -541,7 +573,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -556,7 +589,7 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======
[[webflux-headers-clear-site-data]]
@@ -566,17 +599,17 @@ By default, Spring Security does not add xref:features/exploits/headers.adoc#hea
Consider the following `Clear-Site-Data` header:
.Clear-Site-Data Example
====
----
Clear-Site-Data: "cache", "cookies"
----
====
You can send the `Clear-Site-Data` header on logout:
.Clear-Site-Data Configuration
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -594,7 +627,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -612,4 +646,4 @@ fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
}
}
----
====
======

View File

@@ -13,8 +13,10 @@ If a client makes a request using HTTP rather than HTTPS, you can configure Spri
The following Java configuration redirects any HTTP requests to HTTPS:
.Redirect to HTTPS
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -26,7 +28,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -37,15 +40,17 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
----
====
======
You can wrap the configuration can be wrapped around an `if` statement to be turned on only in production.
Alternatively, you can enable it by looking for a property about the request that happens only in production.
For example, if the production environment adds a header named `X-Forwarded-Proto`, you should use the following Java Configuration:
.Redirect to HTTPS when X-Forwarded
====
.Java
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@@ -59,7 +64,8 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
}
----
.Kotlin
Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@@ -74,7 +80,7 @@ fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain
}
}
----
====
======
[[webflux-hsts]]
== Strict Transport Security