Safeguard
Security Guides•

Does Spring Security protect against session fixation by default?

Yes. Spring Security changes the session ID when a user logs in. What the three strategies do, which one is the default, and how to test the behaviour.

Safeguard Security Team
3 min read

Yes. Spring Security protects against session fixation automatically by creating a new session or changing the session ID when a user logs in. On Servlet 3.1 and newer containers the default strategy is changeSessionId, which uses the container's own protection. You normally only touch it to tighten behaviour.

What session fixation is

Session fixation is an attack where someone gets a valid session ID, persuades a victim to log in using that same ID, and then uses it to act as the victim. The Spring Security reference describes the setup as an attacker creating a session by visiting the site and sending another user a link that carries the identifier.

The defence is simple: never keep the same session ID across the privilege change from anonymous to authenticated.

The three strategies

StrategyWhat it doesDefault for
changeSessionIdKeeps the session but changes its ID using the Servlet containerServlet 3.1 and newer
newSessionCreates a clean session without copying attributes, apart from Spring Security onesNot default
migrateSessionCreates a new session and copies all attributesServlet 3.0 and older

The reference notes that changeSessionId is only available in Servlet 3.1 and newer, and that specifying it in older containers throws an exception. Modern Jakarta Servlet 6 setups are well past that.

Side effects to know

When protection happens, a SessionFixationProtectionEvent is published in the application context. With changeSessionId, HttpSessionIdListener implementations are notified, so the reference advises caution if you use them. Code that cached the session ID before login will see a different ID afterwards.

Steps to verify

  1. Open the app, note the session cookie, log in, and compare the cookie. It should differ.
  2. Check that your configuration has not been changed to weaken the strategy. Read any sessionManagement block in your chain.
  3. If you store sensitive data in the pre login session, use newSession so nothing carries over.
  4. Review listeners that depend on session IDs.
  5. Add an integration test that fails if the ID is unchanged after authentication.
  6. Pair this with other session basics: timeout, secure cookie flags and server side logout. See session timeouts and logout.

A worked example

An attacker opens your login page and receives a session cookie. They send a victim a link that causes the victim's browser to use that same cookie, then wait. The victim logs in. Without protection, the attacker's copy of the cookie is now an authenticated session. With the default protection, the ID changes at login, so the attacker's copy stays anonymous and useless.

That is why the check in step one is worth doing. It takes a minute and confirms a control that is easy to break by accident while changing session settings.

Safeguard scans dependencies, produces SBOMs and uses reachability analysis to show which findings your code can actually reach. You can run a free scan at gold.safeguard.sh/scan.

Quick check

Do I need to configure anything?

Usually not. The default protects on login. Configure only if you want newSession behaviour or have listeners to adjust.

Does token based authentication need this?

It applies to HTTP sessions. If you are fully stateless there is no session to fix.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.