Quick revision
The short answer
In Spring Security 6 the context is loaded by SecurityContextHolderFilter, which, unlike the old SecurityContextPersistenceFilter, no longer saves the context to the HTTP session automatically at the end of the request. Setting the authentication on SecurityContextHolder therefore only lasts for the current request. A custom login endpoint must create a new context, set it on the holder, and save it explicitly with securityContextRepository.saveContext(context, request, response), usually through HttpSessionSecurityContextRepository.
What to remember
In this answer, we saw that a session login is remembered only because the SecurityContext is stored in the HTTP session and loaded on the next request. Spring Security 5's SecurityContextPersistenceFilter saved it automatically, but Spring Security 6's SecurityContextHolderFilter only loads it. A custom endpoint that just sets the authentication therefore loses it at the end of the request. We fixed it by creating a new context, setting it, saving it with HttpSessionSecurityContextRepository and changing the session id, which is what the built-in login filters already do.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Spring Security 6 the context is loaded by SecurityContextHolderFilter, which, unlike the old SecurityContextPersistenceFilter, no longer saves the context to the HTTP session automatically at the end of the request. Setting the authentication on SecurityContextHolder therefore only lasts for the current request. A custom login endpoint must create a new context, set it on the holder, and save it explicitly with securityContextRepository.saveContext(context, request, response), usually through HttpSessionSecurityContextRepository.
Why this question is asked
This bug appears in almost every upgrade from Spring Security 5 to 6 in applications that log users in from their own controller, for example a JSON login endpoint for a single-page application. The code compiles, the login call returns 200, and the very next call is anonymous. Interviewers ask about it because it tests whether we know how a login is remembered between requests, not just how it happens within one. In this answer, we will recap how the security context survives between requests, see what changed in version 6, reproduce the bug, fix it, and see why the built-in login filters never had the problem.
1A Quick Recap of How a Login Is Remembered
HTTP is stateless: each request arrives on its own, and nothing in the protocol ties it to the previous one. A session-based login works because the server stores the SecurityContext, which holds the Authentication of the logged-in user, in the HTTP session, and the browser sends the session cookie (JSESSIONID) with every request.
Within a single request, the context lives in SecurityContextHolder, a ThreadLocal holder. So two moves are needed around every request: load the context from the session into the holder at the start, and save it from the holder back to the session when something changed. The component that reads and writes the stored context is a SecurityContextRepository, and the usual one for sessions is HttpSessionSecurityContextRepository, which keeps it under the session attribute SPRING_SECURITY_CONTEXT.
2What Changed in Spring Security 6
In version 5, SecurityContextPersistenceFilter did both moves. It loaded the context at the start, and it wrapped the response so that, when the request finished or the response was committed, it compared the context with what it had loaded and saved it to the session if it had changed. So any code that called setAuthentication() was saved for free.
In version 6, that filter is replaced by SecurityContextHolderFilter. It only loads the context (lazily, the first time someone asks for it) and it never saves. The reason for the change is that automatic saving was surprising and expensive: it could create sessions nobody asked for, it raced with concurrent requests, and it saved at a moment the code did not control. Now saving is an explicit step, done by whoever performs the authentication.
| Spring Security 5 | Spring Security 6 | |
|---|---|---|
| Filter | SecurityContextPersistenceFilter | SecurityContextHolderFilter |
| Loads the context | Yes, at the start | Yes, lazily on first use |
| Saves the context | Automatically when the request ends | Never; the authenticating code saves it |
| Default repository | HttpSessionSecurityContextRepository | Delegating: request attribute, then session |
3Reproducing the Bug
Here is a typical custom login endpoint written for version 5. It authenticates the user and sets the result on the holder.
9 messages in order. Play them, or step through them one at a time.
Notice that the login itself works: the AuthenticationManager accepts the password, and during this request the holder says the user is authenticated. Then the request ends, FilterChainProxy clears the holder, and nothing ever wrote the context to the session. On the next request SecurityContextHolderFilter loads from the session, finds nothing, and the user is anonymous.
4Fix: Save the Context Explicitly
The fix follows the new rule: create a fresh context, set it on the holder, and save it through a repository.
Notice the three changes. We create an empty context instead of changing the one returned by getContext(), because the existing object may be shared with other threads of the same session, and replacing it avoids races. We call saveContext(), which writes the context into the session under SPRING_SECURITY_CONTEXT, so SecurityContextHolderFilter finds it next time. And we change the session id, which the built-in login does for us through its session strategy; without it, an attacker who planted a session id before login could use it afterwards.
The repository we save to must be one the chain loads from. The default in version 6 is a DelegatingSecurityContextRepository that tries a request attribute first and then the session, so saving to HttpSessionSecurityContextRepository is found on the next request. If the chain is configured with a different repository, we should inject and use that same bean rather than creating a new one.
5Why the Built-In Login Never Had This Problem
UsernamePasswordAuthenticationFilter (form login), the OAuth2 login filter and the other built-in authentication filters already save the context through the SecurityContextRepository that HttpSecurity configured for them. That is why upgrading an application that only uses formLogin() works without changes, and why the bug shows up in exactly the places where we wrote the authentication ourselves: custom login controllers, custom filters, and test helpers that log a user in by hand.
There is also a temporary escape hatch, http.securityContext(c -> c.requireExplicitSave(false)), which brings back the old saving behaviour. It is deprecated and meant only to buy time during an upgrade; check your version, and plan to remove it.
6Follow-up questions
Our API is stateless with JWTs. Do we need saveContext() at all?
No. In a stateless API each request carries its own token, and the bearer token filter authenticates every request, so nothing must survive between requests. Saving to a session would even create sessions we do not want. The explicit save matters only when the login must be remembered on the server side, which means a session.
Why not call SecurityContextHolder.getContext().setAuthentication() at all?
It changes the context object that is already in the holder. In version 5 that object could be the same instance stored in the session and shared by concurrent requests of the same user, so a change on one thread leaked into another. Creating a new context with createEmptyContext() and setting it makes each request own its own object.
How do we find every place that needs fixing during an upgrade?
Search the code for setAuthentication( and setContext( outside Spring Security itself. Each hit that is meant to log a user in for later requests needs a saveContext() call; each hit that only sets a user for the current thread, such as a batch job or a test, is fine as it is.
7Conclusion
In this answer, we saw that a session login is remembered only because the SecurityContext is stored in the HTTP session and loaded on the next request. Spring Security 5's SecurityContextPersistenceFilter saved it automatically, but Spring Security 6's SecurityContextHolderFilter only loads it. A custom endpoint that just sets the authentication therefore loses it at the end of the request. We fixed it by creating a new context, setting it, saving it with HttpSessionSecurityContextRepository and changing the session id, which is what the built-in login filters already do.
Open with
The login only lasts one request because nothing saves the security context any more. In Spring Security 5, SecurityContextPersistenceFilter loaded the context from the session at the start of a request and automatically saved it back at the end if it had changed, so calling setAuthentication on the holder was enough.
Then, if they want more
In version 6 that filter was replaced by SecurityContextHolderFilter, which only loads the context and never saves it; saving became the explicit job of whatever code performs the authentication. So our endpoint authenticates, sets the holder, the request ends, FilterChainProxy clears the ThreadLocal, and the next request loads an empty session and is anonymous.
The fix is to do what the built-in filters do.
I create a new context with createEmptyContext rather than mutating the existing one, set the Authentication, set it on the holder strategy, and then call securityContextRepository.saveContext with the request and response, using HttpSessionSecurityContextRepository or better the same repository bean the chain uses, which by default is a delegating one that checks a request attribute and then the session.
I'd also change the session id after login to prevent session fixation, since the built-in login does that through its session strategy. Form login and OAuth2 login never hit this, because they already save through the configured repository, so after an upgrade I search for manual setAuthentication calls and fix those.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 5, Spring Security 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After moving from Spring Security 5 to 6, a custom login endpoint that calls...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/97716a1fd8/