Quick revision
The short answer
Session fixation is an attack where the attacker makes the victim use a session id the attacker already knows, waits for the victim to log in, and then uses that same id as the victim. Spring Security prevents it by giving the session a new id at the moment of authentication; on servlet containers the default strategy is changeSessionId. SessionCreationPolicy (ALWAYS, IF_REQUIRED, NEVER, STATELESS) controls when Spring Security creates or uses an HTTP session for the security context.
What to remember
In this answer, we saw that session fixation works by making a victim log in on a session id the attacker already knows, and that Spring Security defeats it by changing the session id at authentication, with changeSessionId as the default on servlet containers. We went through the four SessionCreationPolicy values, saw that STATELESS fits bearer-token APIs in their own filter chain, and finished with HttpOnly, Secure and SameSite cookies and short timeouts.
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.
Session fixation is an attack where the attacker makes the victim use a session id the attacker already knows, waits for the victim to log in, and then uses that same id as the victim. Spring Security prevents it by giving the session a new id at the moment of authentication; on servlet containers the default strategy is changeSessionId. SessionCreationPolicy (ALWAYS, IF_REQUIRED, NEVER, STATELESS) controls when Spring Security creates or uses an HTTP session for the security context.
Why this question is asked
Sessions are how a server-rendered application remembers that a user is logged in, and they are also an easy thing to get wrong. Interviewers ask about session fixation because it shows whether we understand what the session id really is, and they ask about SessionCreationPolicy because choosing it wrongly either breaks login or quietly creates sessions on a stateless API. In this answer, we will recap how sessions work, walk through the attack, see how Spring Security defends against it, go through the four policy values, and finish with the cookie settings that protect the session id.
1A Quick Recap of HTTP Sessions
HTTP itself has no memory between requests. A servlet container such as Tomcat gives us one: on the first request that needs it, it creates an HttpSession on the server and sends the browser a cookie, JSESSIONID, holding a random id. On every later request the browser sends the cookie back and the container finds the same session.
Spring Security stores the logged-in user, the SecurityContext, in that session. So after login, whoever presents the session id is treated as that user. The session id is effectively a temporary password, and protecting it is the whole topic of this question.
2How a Session Fixation Attack Works
In a fixation attack the attacker does not steal the victim's session id; they choose it in advance. They visit our site, receive a session id, and then get the victim to use the same id, for example through a link on an old application that accepts the id in the URL (;jsessionid=...), or by setting the cookie through a vulnerable subdomain. The victim then logs in. If the server keeps the same session id across login, the session the attacker knows is now an authenticated session, and the attacker simply uses it.
7 messages in order. Play them, or step through them one at a time.
The flaw is the last step: the id known before login still works after it.
3How Spring Security Prevents It
The defence is simple: never let a session id that existed before authentication survive it. When a user logs in, Spring Security's SessionAuthenticationStrategy gives the session a new id. The id the attacker knows stops working the moment the victim authenticates.
Spring Security offers four settings. changeSessionId, the default on Servlet 3.1 and later containers (which is every current one), keeps the same session object and its attributes but asks the container for a new id. migrateSession creates a new session and copies the attributes. newSession creates a clean session without copying. none turns the protection off, which we should not do.
Notice that we do not have to write any code for this. The protection is on by default, which is why the question is often a test of whether we know it exists and must not be disabled. Old URL rewriting is also worth turning off: in Spring Boot, server.servlet.session.tracking-modes=cookie stops the container from accepting the id in URLs at all.
4What the SessionCreationPolicy Values Mean
SessionCreationPolicy tells Spring Security when it may create a session and whether it reads the security context from one. It does not control the rest of our application: a controller can still call request.getSession() and create one.
IF_REQUIRED, the default, creates a session only when Spring Security needs to store something, such as the user after a form login. ALWAYS creates one on every request, even for anonymous users, which is rarely useful. NEVER means Spring Security never creates a session itself but will use one if something else created it. STATELESS means Spring Security never creates a session and never uses one to find the security context, so each request must authenticate on its own.
| Value | Creates a session? | Uses an existing one? |
|---|---|---|
| ALWAYS | yes, on every request | yes |
IF_REQUIRED (default) | only when it needs to store the context | yes |
| NEVER | no | yes, if the application created one |
| STATELESS | no | no: each request authenticates itself |
STATELESS fits APIs that use bearer tokens, a token sent in the Authorization header on every request. There is nothing to remember, so a session would only cost memory and invite CSRF questions. Notice the securityMatcher: the web pages of the same application can live in another filter chain with IF_REQUIRED and keep their form login, while the API is stateless. The policy is per filter chain, not per application.
5Protecting the Session Cookie
Even with fixation protection, a stolen session id still works. Three cookie attributes make theft harder. HttpOnly stops JavaScript from reading the cookie, so a script injected through an XSS bug cannot copy it. Secure makes the browser send it only over HTTPS. SameSite (Lax or Strict) stops the browser from sending it on most cross-site requests, which also helps against cross-site request forgery.
Notice the timeout too. A short idle timeout limits how long a stolen id is useful, and on logout Spring Security invalidates the session so the id dies immediately.
6Follow-up questions
Why does changeSessionId keep the session attributes?
Because users often put things in the session before logging in, such as a shopping cart or the page they wanted to visit. Keeping the data but changing the id removes the attacker's knowledge of the id without losing the user's work.
Does STATELESS make the application immune to session fixation?
For that filter chain there is no session to fix, so yes. But it also means there is no login state at all, so it only suits requests that authenticate themselves every time, such as bearer tokens or HTTP Basic.
Is CSRF protection still needed with SameSite cookies?
For applications that use cookies for authentication, yes. SameSite is a strong extra layer, but older browsers and some same-site attack paths remain, so Spring Security's CSRF tokens stay on for browser sessions. Stateless APIs that only accept bearer tokens usually disable CSRF, because the browser never attaches the token automatically.
7Conclusion
In this answer, we saw that session fixation works by making a victim log in on a session id the attacker already knows, and that Spring Security defeats it by changing the session id at authentication, with changeSessionId as the default on servlet containers. We went through the four SessionCreationPolicy values, saw that STATELESS fits bearer-token APIs in their own filter chain, and finished with HttpOnly, Secure and SameSite cookies and short timeouts.
Open with
Session fixation is when an attacker gets a victim to use a session id that the attacker already knows, for example through a link with the id in the URL, and then waits for the victim to log in.
Then, if they want more
If the server keeps the same id after login, the attacker's id is now an authenticated session. Spring Security protects against this by changing the session id on authentication.
The default strategy on modern servlet containers is changeSessionId, which keeps the session's attributes but gets a new id from the container; migrateSession and newSession are the alternatives, and none should never be used. I would also turn off URL tracking so ids never appear in links.
SessionCreationPolicy decides when Spring Security creates or uses a session for the security context. IF_REQUIRED is the default and creates one only when needed, for example after form login. ALWAYS creates one on every request. NEVER does not create one but uses an existing one.
STATELESS neither creates nor uses one, so every request must authenticate itself, which is right for bearer-token APIs. It applies per filter chain, so the API can be stateless while the web pages keep sessions. Finally, the session cookie should be HttpOnly, Secure and SameSite Lax or Strict, with a reasonable idle timeout.
About this answer
- Last updated
- 12 November 2026
- 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 "Explain session fixation and how Spring Security protects against it. What do...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/e6ff482fb1/