You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/migration/servlet/session-management.adoc
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,14 @@ In Spring Security 6, the default behavior is that the xref:servlet/authenticati
13
13
Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
14
14
This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
15
15
16
+
[NOTE]
17
+
====
18
+
Saving the context is also needed when clearing it out, for example during logout. Refer to this section to xref:servlet/authentication/session-management.adoc#properly-clearing-authentication[know more about that].
19
+
====
20
+
16
21
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.
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/servlet/authentication/session-management.adoc
+33-6Lines changed: 33 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ But before you leave, consider if any of these use cases fit your application:
12
12
* I want to <<understanding-session-management-components,Understand Session Management's components>>
13
13
* I want to <<ns-concurrent-sessions,restrict the number of times>> a user can be logged in concurrently
14
14
* I want <<store-authentication-manually,to store the authentication directly>> myself instead of Spring Security doing it for me
15
+
* I am storing the authentication manually and I want <<properly-clearing-authentication,to remove it>>
15
16
* I am using <<the-sessionmanagementfilter, `SessionManagementFilter`>> and I need <<moving-away-from-sessionmanagementfilter,guidance on moving away from that>>
16
17
* I want to store the authentication <<customizing-where-authentication-is-stored,in something other than the session>>
17
18
* I am using a <<stateless-authentication, stateless authentication>>, but <<storing-stateless-authentication-in-the-session,I'd still like to store it in the session>>
@@ -84,12 +85,6 @@ By default, Spring Security stores the security context for you in the HTTP sess
84
85
85
86
First, you need to create an implementation of `SecurityContextRepository` or use an existing implementation like `HttpSessionSecurityContextRepository`, then you can set it in `HttpSecurity`.
86
87
87
-
[NOTE]
88
-
====
89
-
The above configuration sets the `SecurityContextRepository` on the `SecurityContextHolderFilter` and **participating** authentication filters, like `UsernamePasswordAuthenticationFilter`.
90
-
To also set it in stateless filters, please see <<storing-stateless-authentication-in-the-session,how to customize the `SecurityContextRepository` for Stateless Authentication>>.
91
-
====
92
-
93
88
[[customizing-the-securitycontextrepository]]
94
89
.Customizing the `SecurityContextRepository`
95
90
====
@@ -134,6 +129,12 @@ open fun filterChain(http: HttpSecurity): SecurityFilterChain {
134
129
----
135
130
====
136
131
132
+
[NOTE]
133
+
====
134
+
The above configuration sets the `SecurityContextRepository` on the `SecurityContextHolderFilter` and **participating** authentication filters, like `UsernamePasswordAuthenticationFilter`.
135
+
To also set it in stateless filters, please see <<storing-stateless-authentication-in-the-session,how to customize the `SecurityContextRepository` for Stateless Authentication>>.
136
+
====
137
+
137
138
If you are using a custom authentication mechanism, you might want to <<store-authentication-manually,store the `Authentication` by yourself>>.
138
139
139
140
[[store-authentication-manually]]
@@ -181,6 +182,32 @@ class LoginRequest {
181
182
And that's it.
182
183
If you are not sure what `securityContextHolderStrategy` is in the above example, you can read more about it in the <<use-securitycontextholderstrategy, Using `SecurityContextStrategy` section>>.
183
184
185
+
[[properly-clearing-authentication]]
186
+
=== Properly Clearing an Authentication
187
+
188
+
If you are using Spring Security's xref:servlet/authentication/logout.adoc[Logout Support] then it handles a lot of stuff for you including clearing and saving the context.
189
+
But, let's say you need to manually log users out of your app. In that case, you'll need to make sure you're clearing and saving the context properly.
190
+
191
+
Now, you might already be familiar with clearing the `SecurityContextHolder` by doing `SecurityContextHolderStrategy#clearContext()`.
192
+
That's great, but if your app requires an xref:migration/servlet/session-management.adoc#_require_explicit_saving_of_securitycontextrepository[explicit save of the context], simply clearing it isn't enough.
193
+
The reason is that it doesn't remove it from the `SecurityContextRepository`, which means the `SecurityContext` could still be available for the next requests, and we definitely don't want that.
194
+
195
+
To make sure the authentication is properly cleared and saved, you can invoke {security-api-url}/org/springframework/security/web/authentication/logout/SecurityContextLogoutHandler.html[the `SecurityContextLogoutHandler`] which does that for us, like so:
196
+
197
+
====
198
+
.Java
199
+
[source,java,role="primary"]
200
+
----
201
+
SecurityContextLogoutHandler handler = new SecurityContextLogoutHandler(); <1>
<1> Create a new instance of `SecurityContextLogoutHandler`
207
+
<2> Call the `logout` method passing in the `HttpServletRequest`, `HttpServletResponse` and a `null` authentication because it is not required for this handler.
208
+
209
+
It's important to remember that clearing and saving the context is just one piece of the logout process, therefore we recommend having Spring Security take care of it.
210
+
184
211
[[stateless-authentication]]
185
212
=== Configuring Persistence for Stateless Authentication
0 commit comments