【发布时间】:2019-10-16 01:24:26
【问题描述】:
使用 Spring-boot 安全性,我想强制在特定时间(例如 15 分钟)后使记录的用户会话到期。
由于JSESSIONID cookie 用于识别登录用户,我希望这个 cookie 必须强制到期。如果这是正确的,该怎么做?如果不是,正确的做法是什么?
我现在的SecurityConfig:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("userDetailsServiceImpl")
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/login").permitAll()
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.logout().logoutUrl("/logout").deleteCookies("JSESSIONID").clearAuthentication(true).invalidateHttpSession(true)
.and()
.httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider()).eraseCredentials(true);
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(encoder());
return authProvider;
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(11);
}
}
【问题讨论】:
-
通常会话过期设置在 App Server 中,或者在 web.xml 中的 WebApp 层中设置
-
通过 application.properties 使用 spring.session.timeout = 900
-
@DirkDeyne 我试图设置
spring.session.timeout = 10。然后我记录发送经过身份验证的请求以获取 JSESSIONID cookie。然后尝试未经授权发送请求,只是使用JSESSIONID进行身份验证。即使超时已经过期,请求也会被识别为授权并返回数据。 JSESSIONID cookie 的到期时间设置为 19 年后。 -
@Radouxca 对不起,试试 server.servlet.session.cookie.max-age ?
-
@DirkDeyne server.servlet.session.cookie.max-age 运行良好。但是,此属性是为所有 cookie 设置 max-age,还是只为会话 cookie 设置 max-age?
标签: spring spring-boot spring-security