Skip to content

Commit 3da0d1b

Browse files
committed
Merge branch '5.8.x'
2 parents 83093ec + aac1261 commit 3da0d1b

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

docs/modules/ROOT/pages/migration.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ Also, this guide includes ways to <<revert,revert to 5.x>> behaviors and its def
1717

1818
== Servlet
1919

20+
In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].
21+
Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
22+
Unfortunately, automatic persistence of the `SecurityContext` can surprise users when it is done prior to the request completing (i.e. just prior to committing the `HttpServletResponse`).
23+
It also is complex to keep track of the state to determine if a save is necessary causing unnecessary writes to the `SecurityContextRepository` (i.e. `HttpSession`) at times.
24+
25+
In Spring Security 6, the default behavior is that the xref:servlet/authentication/persistence.adoc#securitycontextholderfilter[`SecurityContextHolderFilter`] will only read the `SecurityContext` from `SecurityContextRepository` and populate it in the `SecurityContextHolder`.
26+
Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
27+
This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
28+
29+
If you are explicitly opting into Spring Security 6's new defaults, the following configuration can be removed to accept the Spring Security 6 defaults.
30+
31+
include::partial$servlet/architecture/security-context-explicit.adoc[]
32+
2033
[[requestcache-query-optimization]]
2134
=== Optimize Querying of `RequestCache`
2235

docs/modules/ROOT/pages/servlet/authentication/persistence.adoc

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,8 @@ image::{figures}/securitycontextholderfilter.png[]
144144
<1> Before running the rest of the application, `SecurityContextHolderFilter` loads the `SecurityContext` from the `SecurityContextRepository` and sets it on the `SecurityContextHolder`.
145145
<2> Next, the application is ran.
146146

147-
Unlike, xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersisteneFilter`], `SecurityContextHolderFilter` only loads the `SecurityContext` it does not save the `SecurityContext`.
147+
Unlike, xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`], `SecurityContextHolderFilter` only loads the `SecurityContext` it does not save the `SecurityContext`.
148148
This means that when using `SecurityContextHolderFilter`, it is required that the `SecurityContext` is explicitly saved.
149149

150-
.Explicit Saving of SecurityContext
151-
====
152-
.Java
153-
[source,java,role="primary"]
154-
----
155-
public SecurityFilterChain filterChain(HttpSecurity http) {
156-
http
157-
// ...
158-
.securityContext((securityContext) -> securityContext
159-
.requireExplicitSave(true)
160-
);
161-
return http.build();
162-
}
163-
----
164150

165-
.XML
166-
[source,xml,role="secondary"]
167-
----
168-
<http security-context-explicit-save="true">
169-
<!-- ... -->
170-
</http>
171-
----
172-
====
151+
include::partial$servlet/architecture/security-context-explicit.adoc[]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.Explicit Saving of SecurityContext
2+
====
3+
.Java
4+
[source,java,role="primary"]
5+
----
6+
public SecurityFilterChain filterChain(HttpSecurity http) {
7+
http
8+
// ...
9+
.securityContext((securityContext) -> securityContext
10+
.requireExplicitSave(true)
11+
);
12+
return http.build();
13+
}
14+
----
15+
16+
.Kotlin
17+
[source,kotlin,role="secondary"]
18+
----
19+
@Bean
20+
open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
21+
http {
22+
securityContext {
23+
requireExplicitSave = true
24+
}
25+
}
26+
return http.build()
27+
}
28+
----
29+
30+
.XML
31+
[source,xml,role="secondary"]
32+
----
33+
<http security-context-explicit-save="true">
34+
<!-- ... -->
35+
</http>
36+
----
37+
====
38+
39+
40+
Upon using the configuration, it is important that any code that sets the `SecurityContextHolder` with a `SecurityContext` also saves the `SecurityContext` to the `SecurityContextRepository` if it should be persisted between requests.
41+
42+
For example, the following code:
43+
44+
.Setting `SecurityContextHolder` with `SecurityContextPersistenceFilter`
45+
====
46+
.Java
47+
[source,java,role="primary"]
48+
----
49+
SecurityContextHolder.setContext(securityContext);
50+
----
51+
52+
.Kotlin
53+
[source,kotlin,role="secondary"]
54+
----
55+
SecurityContextHolder.setContext(securityContext)
56+
----
57+
====
58+
59+
should be replaced with
60+
61+
.Setting `SecurityContextHolder` with `SecurityContextHolderFilter`
62+
====
63+
.Java
64+
[source,java,role="primary"]
65+
----
66+
SecurityContextHolder.setContext(securityContext);
67+
securityContextRepository.saveContext(securityContext, httpServletRequest, httpServletResponse);
68+
----
69+
70+
.Kotlin
71+
[source,kotlin,role="secondary"]
72+
----
73+
SecurityContextHolder.setContext(securityContext)
74+
securityContextRepository.saveContext(securityContext, httpServletRequest, httpServletResponse)
75+
----
76+
====

0 commit comments

Comments
 (0)