Spring Session集成Redis时server.session.timeout配置无效问题咨询
Let's break down why your server.session.timeout setting isn't working and how to fix it quickly.
The Root Cause
When you use Spring Session with Redis (or any external session store), the standard server.session.timeout property gets ignored. This is because Spring Session replaces the Servlet container's (like Tomcat) default session management with its own SessionRepositoryFilter, which doesn't respect container-level timeout configurations.
Fix: Use Spring Session's Native Configuration
Replace your server.session.timeout=10 with the Spring Session-specific timeout property. Note that the value supports Duration format (recommended for clarity) or raw numbers (interpreted as seconds by default):
# application.properties spring.session.store-type=redis # Set session timeout to 10 seconds (explicit unit avoids ambiguity) spring.session.timeout=10s
If you're using an older version of Spring Session (pre-2.4), you might need the Redis-specific property instead:
spring.session.redis.timeout=10s
Verify the Fix
After restarting your app:
- Send a new request to create a session.
- In Redis, run
TTL spring:session:sessions:<your-session-id>to check the time-to-live value. It should be close to 10 seconds. - You can also check the expires key:
TTL spring:session:sessions:expires:<your-session-id>— this should match the session timeout, as it triggers the session cleanup in Redis.
Additional Checks
- Double-check your dependencies: Ensure you have
spring-boot-starter-sessionandspring-boot-starter-data-redisin your build file (Spring Boot handles the integration automatically). - Confirm Redis isn't overriding TTL: Make sure your Redis server doesn't have a global default TTL that's overriding the app's configuration (this is rare, but worth verifying).
内容的提问来源于stack exchange,提问作者nagy.zsolt.hun

