Session management in Spring combines the Servlet container's HttpSession with three layers: Spring MVC (HttpSession parameters and @SessionAttributes), Spring Boot (timeout and cookie properties) and Spring Security (session fixation protection, concurrent session limits and logout handling). For several servers, Spring Session moves session storage to Redis or a database.
This guide shows how each layer works in a Spring Boot 3 application and which settings to change first.
Spring MVC can inject the session into a controller method, so you rarely call request.getSession() yourself:
Spring Boot 3 uses jakarta.servlet packages; older code with javax.servlet fails to compile on Boot 3. Declaring an HttpSession parameter creates a session on first use, so avoid it on endpoints that do not need state.
You can also keep state in a bean with session scope. Spring creates one instance per user session and injects a proxy:
@SessionAttributes keeps a model attribute in the session across several requests of one controller, which suits multi-step forms:
Always call setComplete() when the flow ends, or the attribute stays in the session until it expires.
Boot exposes the common settings as properties:
The timeout is the idle time between requests, not an absolute lifetime. Tomcat rounds it to whole minutes. HttpOnly, Secure and SameSite protect the session cookie from script access, plain HTTP transport and cross-site requests. Do not put session IDs in URLs.
Spring Security controls how sessions are created, protected and limited. A current configuration uses the lambda DSL:
What each part does:
changeSessionId), so an attacker cannot reuse an ID they planted before login.maximumSessions(1): a second login expires the first session. With maxSessionsPreventsLogin(true), the second login is rejected instead.HttpSessionEventPublisher: lets Spring Security learn when sessions are destroyed, which concurrency control needs.STATELESS for token-based APIs so no session is created or used.In-memory sessions are lost on restart and are not shared across instances. Spring Session stores them externally. With Redis, add spring-session-data-redis and a Redis connection:
Spring Boot auto-configures Spring Session when the dependency is present, and the HttpSession API in your code does not change. JDBC, MongoDB and Hazelcast stores also exist. Session attributes must be serializable, so keep them small and prefer storing IDs instead of full entities.
| Option | Best for |
|---|---|
| In-memory | Single instance, development |
| Sticky sessions | Quick scaling when losing sessions on failure is acceptable |
| Spring Session with Redis | Several instances, fast shared state |
| Stateless tokens | APIs and mobile clients |
Set server.servlet.session.timeout, for example 30m. With Spring Session, use spring.session.timeout.
Set SessionCreationPolicy.STATELESS in sessionManagement. Also avoid declaring HttpSession parameters, which create sessions on demand.
An attacker gives a victim a known session ID and then uses it after the victim logs in. Changing the session ID on authentication, which Spring Security does by default, prevents this.
No. It becomes useful when you run several instances, need sessions to survive restarts or want to list and invalidate sessions centrally.
Use HttpSession, session-scoped beans or @SessionAttributes for per-user state in Spring MVC, set timeout and cookie flags through Spring Boot properties and let Spring Security handle fixation, concurrency limits and logout. Add Spring Session with Redis when several instances must share sessions, or use stateless tokens for APIs.