具有自定义会话超时的Spring Security

Jij*_*mar 5 java session spring-security session-timeout spring-boot

我需要一个从GUI设置会话超时的选项.目前,我们可以使用配置全局更改会话超时

server.session.timeout=120
server.session.cookie.max-age=120
server.session.timeout=120`
Run Code Online (Sandbox Code Playgroud)

我们还可以为每个会话设置会话超时.

session.setMaxInactiveInterval(120);
Run Code Online (Sandbox Code Playgroud)

但是没有找到全局设置会话超时的选项.有没有办法用弹簧靴做到这一点

提前致谢

sha*_*ool 2

我认为您可能需要使用 spring jdbc 会话或 redis 会话,以便您可以完全控制会话存储。

Spring Boot jdbc 会话给出一个 bean

@Autowired JdbcOperationsSessionRepository sessionRepository;

使用它我们可以从控制器设置空闲超时。

只需添加依赖项并为您的配置添加 @EnableJdbcHttpSession 即可。

http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc-boot.html#httpsession-jdbc-boot-sample

但看起来会话表不会自动创建,您需要手动创建表。你可以在以下位置找到语句

org/springframework/session/jdbc/schema-*.sql

http://docs.spring.io/spring-session/docs/current/api/org/springframework/session/jdbc/JdbcOperationsSessionRepository.html

编辑:1

即使 jdbc 会话提供了设置全局默认超时的方法,我发现它无法正常工作。似乎唯一的解决方案是在用户首次登录时使用以下代码设置会话超时。

session.setMaxInactiveInterval(120);
Run Code Online (Sandbox Code Playgroud)