spring security这样设置过期时间怎么起不到作用 spring.security.session.timeout,时间是6000
本问题来自阿里云开发者社区的【11大垂直技术领域开发者社群】。https://developer.aliyun.com/article/706511 点击链接欢迎加入感兴趣的技术领域群。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在Spring Security中设置会话过期时间,通常不是直接通过spring.security.session.timeout
这个属性来配置的。这个属性可能不会直接被Spring Security识别,特别是如果你使用的是较新版本或者标准的配置方式。正确的做法是通过Spring Security的配置类或XML配置来设定会话超时时间。
如果你的应用使用Java配置方式,可以通过重写WebSecurityConfigurerAdapter
中的configure(HttpSecurity http)
方法来设置会话超时时间。示例如下:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.maximumSessions(1) // 如果需要限制最大会话数,可以设置
.maxSessionsPreventsLogin(true) // 达到最大会话时是否阻止新的登录
.expiredUrl("/login?expired") // 会话过期后重定向的URL
.and()
.invalidSessionUrl("/login?invalid") // 无效会话访问的URL
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
// 其他安全配置...
.logout() // 配置注销等
.and()
.authorizeRequests() // 授权配置
// ...其他配置
;
}
}
在这个例子中,并没有直接设置一个具体的超时时间(比如6000秒),因为Spring Security默认依赖于应用服务器(如Tomcat)的会话超时设置。要设置具体的超时时间,你需要调整应用服务器的会话超时配置。
对于基于Tomcat的应用服务器,你可以在application.properties
或application.yml
中添加如下配置来设置会话超时时间(单位为秒):
server.servlet.session.timeout=6000
或者在YAML格式中:
server:
servlet:
session:
timeout: 6000s
这样设置后,Tomcat将会话的最长不活动时间设置为6000秒。