SessionManagementConfigurer is the Spring Security class behind the sessionManagement section of the HttpSecurity DSL. It configures how the framework creates and tracks HTTP sessions: the session creation policy, session fixation protection, the limit on concurrent sessions per user and what happens to an invalid or expired session. You rarely instantiate it yourself, you reach it through http.sessionManagement(...).
The examples below use the lambda DSL of Spring Security 6.x, which is also the only form that remains in 7.x. The no-argument sessionManagement() with and() chaining was deprecated in 6.1 and removed in 7.0.
HttpSecurity builds a SecurityFilterChain. Each feature has a configurer that adds filters to the chain. SessionManagementConfigurer registers, among others, SessionManagementFilter, ConcurrentSessionFilter (when you set a session limit) and the strategies that run after authentication.
This keeps one active session per user and lets a new login expire the older session.
sessionCreationPolicy decides whether Spring Security creates an HttpSession.
| Policy | Meaning |
|---|---|
IF_REQUIRED | Default. Creates a session only when needed |
ALWAYS | Always creates a session |
NEVER | Never creates one, but uses an existing one |
STATELESS | Never creates or uses a session for the security context |
Use STATELESS for REST APIs secured with bearer tokens such as JWTs. The application server can still create a session for other reasons, for example if your own code calls request.getSession().
A session fixation attack plants a known session ID in the victim's browser before login. Spring Security defends by changing the session ID when the user authenticates. The options are:
changeSessionId(): the default on Servlet 3.1 and newer, keeps the session and its attributes but issues a new ID.newSession(): creates a new session and does not copy attributes.migrateSession(): creates a new session and copies the attributes.none(): disables the protection, which you should not do.maximumSessions(n) limits how many sessions one principal may hold at once. When the limit is reached, one of two things happens:
maxSessionsPreventsLogin(false) (default): the oldest session is marked expired and its next request is redirected to the expiredUrl.maxSessionsPreventsLogin(true): the new login is rejected.For this to work, Spring Security must learn when sessions end. Register a HttpSessionEventPublisher bean, otherwise logged-out or timed-out sessions stay counted:
To inspect or expire sessions yourself, supply a shared SessionRegistry with .sessionRegistry(registry). The default registry is in memory and per JVM, so with several instances use Spring Session so the data is shared.
invalidSessionUrl("/login?invalid") redirects a request that arrives with an unknown or expired session ID. For finer control use invalidSessionStrategy(...).
The timeout itself is not set by this configurer. It belongs to the servlet container; in Spring Boot set it in application.properties:
You can also harden the cookie with server.servlet.session.cookie.http-only=true, server.servlet.session.cookie.secure=true and server.servlet.session.cookie.same-site=lax.
Set SessionCreationPolicy.STATELESS and nothing else. Concurrent session limits and fixation protection have no meaning without sessions.
maximumSessions limit ignored?Usually the HttpSessionEventPublisher bean is missing, or the principal's equals and hashCode are not implemented, so the registry sees different users.
sessionManagement().and() still valid?No. Use the lambda form. The chained style was deprecated in Spring Security 6.1 and removed in 7.0.
Only with a shared store. Add Spring Session (Redis, JDBC) and a distributed SessionRegistry such as SpringSessionBackedSessionRegistry.
SessionManagementConfigurer controls how Spring Security uses HTTP sessions: creation policy, fixation protection, concurrent session limits and invalid session handling. Configure it with the lambda DSL in sessionManagement(...), publish HttpSessionEventPublisher when you limit sessions, and set the timeout through the container or Spring Boot properties.