Spring boot Spring启动会话超时

Spring boot Spring启动会话超时,spring-boot,Spring Boot,server.session timeout似乎只适用于嵌入式tomcat 我放了一个log语句来检查会话的最大间隔时间。在将war文件手动部署到tomcat之后,我意识到默认会话超时值(30分钟)仍在使用 如何使用spring boot设置会话超时值(不是针对嵌入式tomcat,而是针对独立应用程序服务器)?将spring boot应用程序部署到独立服务器时,配置会话超时的方式与任何其他war部署中的方式相同 对于Tomcat,您可以通过在server.xml中的manager元素上配置max

server.session timeout
似乎只适用于嵌入式tomcat

我放了一个log语句来检查会话的最大间隔时间。在将war文件手动部署到tomcat之后,我意识到默认会话超时值(30分钟)仍在使用


如何使用spring boot设置会话超时值(不是针对嵌入式tomcat,而是针对独立应用程序服务器)?

将spring boot应用程序部署到独立服务器时,配置会话超时的方式与任何其他war部署中的方式相同


对于Tomcat,您可以通过在
server.xml
中的manager元素上配置
maxInactiveInterval
属性或使用web.xml中的
session timeout
元素来设置会话超时。请注意,第一个选项将影响部署到Tomcat实例的每个应用程序。

您已经发现,与我一样,Servlet API和Spring API中都没有用于设置会话超时的直接调用。这里和那里都在讨论它的必要性,但它还没有得到解决

有一种循环的方式来做你想做的事。您可以配置设置会话超时的会话侦听器。我在以下网站上看到了一篇包含代码示例的文章:

我希望这能有所帮助。

[以防万一有人觉得这有用]

如果您使用的是Spring Security,则可以扩展该类并在身份验证成功处理程序中设置会话超时:

public类noredidirectsavedrequestawareauthenticationsuccesshandler
扩展SimpleRuthenticationSuccessHandler{
公共最终整数会话超时(秒)=60*30;
@凌驾
验证成功时公共无效(HttpServletRequest请求,
HttpServletResponse,
身份验证(身份验证)
抛出ServletException、IOException{
request.getSession().setMaxInactiveInterval(会话超时,以秒为单位);
// ...
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests()
.anyRequest()
.authenticated()
.及()
.formLogin()
.loginProcessingUrl(“/login”)
.successHandler(新的NoRedirectSavedRequestAwareAuthenticationSuccessHandler())
.failureHandler(新的SimpleRuthenticationFailureHandler())
.和().httpBasic();
}
}

在您的应用程序中。属性

#session timeout (in secs for spring, in minutes for tomcat server/container)
server.session.timeout=1
我测试了它,正在工作!
事实证明,tomcat在几分钟内获取了该属性。根据justin的回答,该回答显示了如何使用带有Spring Security的
AuthenticationSuccessHandler设置会话超时,我创建了一个
SessionTimeoutAuthSuccessHandler

public class SessionTimeoutAuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  public final Duration sessionTimeout;

  public SessionTimeoutAuthSuccessHandler(Duration sessionTimeout) {
    this.sessionTimeout = sessionTimeout;
  }

  @Override
  public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication auth) throws ServletException, IOException {
    req.getSession().setMaxInactiveInterval(Math.toIntExact(sessionTimeout.getSeconds()));
    super.onAuthenticationSuccess(req, res, auth);
  }
}
使用中:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
      .anyRequest().authenticated()
      .and().formLogin().loginPage("/login")
      .successHandler(new SessionTimeoutAuthSuccessHandler(Duration.ofHours(8))).permitAll()
      .and().logout().logoutUrl("/logout").permitAll();   
  }
...
}

编辑
SavedRequestAwareAuthenticationSuccessHandler
扩展而非
simplerulauthenticationsuccesshandler
,以确保重新身份验证后原始请求不会丢失。

使用
HttpSessionListener

@Configuration
public class MyHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        event.getSession().setMaxInactiveInterval(30);
    }
}

作为对@Ali answer的补充,您还可以在
application.yml
文件中创建一个
session.timeout
变量,并在类中使用它。这对于Spring Boot war和外部Tomcat应该非常有用:

application.yml

SessionListener(带有
@配置
注释)


是的,我也看过这篇文章。如果应用程序不使用web.xml,而是在@SpringBootApplication/SpringBootServletInitializer中使用org.springframework.boot.web.servlet.FilterRegistrationBean,该怎么办?然后,您可以像我前面所说的那样在server.xml中配置它,或者直接通过中的servlet API()配置它。只是想检查SpringWeb/Boot是否支持此功能。谢谢在spring引导中,我发现“即使会话超时可以通过将application.properties中的server.sessiontimeout属性设置为所需的秒值来轻松配置”。这是真的吗?如果您只是在属性文件中添加会话超时,它会工作吗?是的,当您将spring boot项目打包到war中,并将其部署到外部容器中时,spring boot会话管理将使用org.apache.catalina.session.StandardManage。aka,它将使用包含的web.xml会话过期时间。我最近阅读了有关的内容,在版本1.5.7中它被称为
server.session.timeout
。对于Springboot 2.1.x,它现在是:server.servlet.session.timeout。更多信息,请参阅。这适用于独立tomcat还是仅适用于嵌入式tomcat?这仅适用于嵌入式“for spring”是什么意思?当使用spring Framework作为独立应用程序运行时,应在秒中指定。问题是关于“将war文件手动部署到tomcat”而不是嵌入式tomcat。
  session:
    timeout: 480 # minutes
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
class SessionListener implements HttpSessionListener {

    @Value("${session.timeout}")
    private Integer sessionTimeout;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        event.getSession().setMaxInactiveInterval(sessionTimeout);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {}

}