此问题的原因可能是服务器上的时区设置不正确,在代码中设置的会话超时时间与实际情况不匹配。要解决这个问题,可以尝试以下几个步骤:
System.out.println(TimeZone.getDefault().getID());
如果时区设置不正确,可以在服务器上更改或使用如下代码进行修正:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.invalidSessionUrl("/login?timeout")
.maximumSessions(1)
.expiredUrl("/login?expired")
.maxSessionsPreventsLogin(true)
.and()
.sessionFixation().none();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
在这里,您可以明确设置会话的最大时间和过期时间,以确保它们在服务器上正确工作。
public void setSessionTimeOut(int mins) {
int secs = mins * 60;
request.getSession().setMaxInactiveInterval(secs);
}
这将在代码中更改会话的最大空闲间隔,以匹配您的实际情况。