Skip to content

Commit eb58655

Browse files
Improve Docs by mentioning that Empty SecurityContext should be saved
Closes gh-12906
1 parent c15589e commit eb58655

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

docs/modules/ROOT/pages/migration/servlet/session-management.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ In Spring Security 6, the default behavior is that the xref:servlet/authenticati
1111
Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
1212
This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
1313

14+
[NOTE]
15+
====
16+
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].
17+
====
18+
1419
To opt into the new Spring Security 6 default, the following configuration can be used.
1520

1621
include::partial$servlet/architecture/security-context-explicit.adoc[]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ If not configured a status code 200 will be returned by default.
139139
[[jc-logout-references]]
140140
== Further Logout-Related References
141141

142+
- xref:servlet/authentication/session-management.adoc#properly-clearing-authentication[Properly Clearing Authentication When Explicit Save Is Enabled]
142143
- <<ns-logout, Logout Handling>>
143144
- xref:servlet/test/mockmvc/logout.adoc#test-logout[ Testing Logout]
144145
- xref:servlet/integrations/servlet-api.adoc#servletapi-logout[ HttpServletRequest.logout()]

docs/modules/ROOT/pages/servlet/authentication/session-management.adoc

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ But before you leave, consider if any of these use cases fit your application:
5454
* I want to <<understanding-session-management-components,Understand Session Management's components>>
5555
* I want to <<ns-concurrent-sessions,restrict the number of times>> a user can be logged in concurrently
5656
* I want <<store-authentication-manually,to store the authentication directly>> myself instead of Spring Security doing it for me
57+
* I am storing the authentication manually and I want <<properly-clearing-authentication,to remove it>>
5758
* I am using <<the-sessionmanagementfilter, `SessionManagementFilter`>> and I need <<moving-away-from-sessionmanagementfilter,guidance on moving away from that>>
5859
* I want to store the authentication <<customizing-where-authentication-is-stored,in something other than the session>>
5960
* 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>>
@@ -168,12 +169,6 @@ By default, Spring Security stores the security context for you in the HTTP sess
168169

169170
First, you need to create an implementation of `SecurityContextRepository` or use an existing implementation like `HttpSessionSecurityContextRepository`, then you can set it in `HttpSecurity`.
170171

171-
[NOTE]
172-
====
173-
The above configuration sets the `SecurityContextRepository` on the `SecurityContextHolderFilter` and **participating** authentication filters, like `UsernamePasswordAuthenticationFilter`.
174-
To also set it in stateless filters, please see <<storing-stateless-authentication-in-the-session,how to customize the `SecurityContextRepository` for Stateless Authentication>>.
175-
====
176-
177172
[[customizing-the-securitycontextrepository]]
178173
.Customizing the `SecurityContextRepository`
179174
====
@@ -220,6 +215,12 @@ open fun filterChain(http: HttpSecurity): SecurityFilterChain {
220215
----
221216
====
222217

218+
[NOTE]
219+
====
220+
The above configuration sets the `SecurityContextRepository` on the `SecurityContextHolderFilter` and **participating** authentication filters, like `UsernamePasswordAuthenticationFilter`.
221+
To also set it in stateless filters, please see <<storing-stateless-authentication-in-the-session,how to customize the `SecurityContextRepository` for Stateless Authentication>>.
222+
====
223+
223224
If you are using a custom authentication mechanism, you might want to <<store-authentication-manually,store the `Authentication` by yourself>>.
224225

225226
[[store-authentication-manually]]
@@ -267,6 +268,32 @@ class LoginRequest {
267268
And that's it.
268269
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>>.
269270

271+
[[properly-clearing-authentication]]
272+
=== Properly Clearing an Authentication
273+
274+
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.
275+
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.
276+
277+
Now, you might already be familiar with clearing the `SecurityContextHolder` by doing `SecurityContextHolderStrategy#clearContext()`.
278+
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.
279+
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.
280+
281+
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:
282+
283+
====
284+
.Java
285+
[source,java,role="primary"]
286+
----
287+
SecurityContextLogoutHandler handler = new SecurityContextLogoutHandler(); <1>
288+
handler.logout(httpServletRequest, httpServletResponse, null); <2>
289+
----
290+
====
291+
292+
<1> Create a new instance of `SecurityContextLogoutHandler`
293+
<2> Call the `logout` method passing in the `HttpServletRequest`, `HttpServletResponse` and a `null` authentication because it is not required for this handler.
294+
295+
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.
296+
270297
[[stateless-authentication]]
271298
=== Configuring Persistence for Stateless Authentication
272299

0 commit comments

Comments
 (0)