springboot server.servlet.session.timeout

在 Spring Boot 中,可以通过设置 server.servlet.session.timeout 属性来控制会话超时时间。这个属性指定了会话在没有活动多长时间后就会超时。

可以在 application.properties 或 application.yml 中设置该属性,例如:

server.servlet.session.timeout=30m

上面的设置将会话超时时间设置为 30 分钟。您也可以使用 s(秒)、m(分钟)、h(小时)或 d(天)来指定超时时间。

在 Spring Boot 2.3.0 及更高版本中,您还可以使用 server.servlet.session.persistent 属性来控制会话是否持久化。如果设置为 true,则会话将在用户关闭浏览器后继续保留,直到超时。默认情况下,会话是非持久的。

如果您希望在代码中设置会话超时时间,则可以使用 ServerProperties 类,例如:

@Value("${server.servlet.session.timeout}")
private Duration sessionTimeout;
@Bean
public ServletWebServerFactory servletContainer(ServerProperties serverProperties) {
   TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
   tomcat.getSession().setTimeout(sessionTimeout);
   return tomcat;

希望这些信息能帮助您。

  •