spring 引导中的 servlet 超时不正确
Timeouts are not correct for servlet in spring boot
这是远程服务器属性:
server.servlet.session.timeout=3m
我的 local.properties
我们也有这样的配置:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/login?invalidSession")//dokunma
.maximumSessions(1)//
.maxSessionsPreventsLogin(true)//
.expiredUrl("/login?expired")
.sessionRegistry(sessionRegistry());
我们有一个 class 这样的:
@Bean // Http Listener
public HttpSessionListener httpSessionListener() {
return new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
我这样做是为了查看内部时间。
但是在服务器上,我看到这个日志:
sessionCreated sessionid: 342E6139B2FE108D26537C9D684FBFF3, setMaxInactiveInterval: 1800, ipaddress: null
一定是180,不是1800,为什么会相乘?
我们没有任何其他代码来设置它。例如:
request.getSession(false).setMaxInactiveInterval(11);
我们没有这个。但如果我找不到任何解决方案,我会使用它。
例如,对于远程,我更改为:
server.servlet.session.timeout=44s
但我看到的是:
sessionCreated sessionid: 7C3573FE7B5FB6C8939DF8BF60B1B550, setMaxInactiveInterval: 1800, ipaddress: null
Tomcat9是干这个的?
在我的本地,我使用该属性进行测试。
所以
server.servlet.session.timeout=44s
对于我本地的本地和远程服务器数据库配置。
但是这次:
sessionCreated sessionid: 747E6BF3DCD061DFF306325FE4FD76B6, getMaxInactiveInterval: 60, ipaddress: 0:0:0:0:0:0:0:1
747E6BF3DCD061DFF306325FE4FD76B6 0:0:0:0:0:0:0:1 Session Created
我做错了什么?
对于上次测试,我将其添加到本地但具有远程属性的成功处理程序中:
LoggerService.logger.info("onAuthenticationSuccess sessionid: {}, getMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
request.getSession(false).setMaxInactiveInterval(55);
LoggerService.logger.info("onAuthenticationSuccess sessionid: {}, getMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
如果我输入用户名密码,我可以看到:
: onAuthenticationSuccess sessionid: F796EA6C54D8BCA239A36E02C4A7A030, getMaxInactiveInterval: 60, ipaddress: 0:0:0:0:0:0:0:1
: onAuthenticationSuccess sessionid: F796EA6C54D8BCA239A36E02C4A7A030, getMaxInactiveInterval: 55, ipaddress: 0:0:0:0:0:0:0:1
我也是这样做的:
@Bean // Http Listener
public HttpSessionListener httpSessionListener() {
return new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
session.setMaxInactiveInterval(55);
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
又是一样的:
sessionCreated sessionid: FFA7DC9A6558951F1CB790AD9D804F88, getMaxInactiveInterval: 60, ipaddress: null
sessionCreated sessionid: FFA7DC9A6558951F1CB790AD9D804F88, getMaxInactiveInterval: 55, ipaddress: null
FFA7DC9A6558951F1CB790AD9D804F88 0:0:0:0:0:0:0:1 Session Created
对于远程,我使用相同的代码进行了测试并且它也有效但我不想以编程方式设置
sessionCreated before sessionid: 38EC29F7C9C45B34D1FDF05B1F90DC3A, getMaxInactiveInterval: 1800, ipaddress: 192.ss
sessionCreated after sessionid: 38EC29F7C9C45B34D1FDF05B1F90DC3A, getMaxInactiveInterval: 180, ipaddress: 192.ss
所以,有两个问题:
- 为什么 application-remote-properties 超时值不适用于本地?
- 为什么远程超时是10倍(properties有3m但是log显示1800s)
server.*
属性用于控制 Boot 使用的 嵌入式容器。 Spring Boot 将使用 ServletWebServerFactory
实例之一创建 servlet 容器实例。这些 类 使用 server.*
属性来配置受控的 servlet 容器(tomcat、jetty 等)。
但是,当您将应用程序作为 war
文件部署到 Tomcat 实例时,server.*
属性不适用。它们不适用,因为预配置的 servlet 容器可用(因为它是远程 运行 服务)。因此部署到远程 Tomcat 将使 server.*
属性无用。
关于以分钟为单位的会话超时。 Spring开机会convert the session.servlet.session.timeout
property to minutes,所以44s
或55s
会自动转换为1分钟。将它设置为少于一分钟也没有多大意义,因为 Tomcat 每分钟都会使线程 运行 的会话无效。
这是远程服务器属性:
server.servlet.session.timeout=3m
我的 local.properties
我们也有这样的配置:
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.invalidSessionUrl("/login?invalidSession")//dokunma
.maximumSessions(1)//
.maxSessionsPreventsLogin(true)//
.expiredUrl("/login?expired")
.sessionRegistry(sessionRegistry());
我们有一个 class 这样的:
@Bean // Http Listener
public HttpSessionListener httpSessionListener() {
return new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
我这样做是为了查看内部时间。
但是在服务器上,我看到这个日志:
sessionCreated sessionid: 342E6139B2FE108D26537C9D684FBFF3, setMaxInactiveInterval: 1800, ipaddress: null
一定是180,不是1800,为什么会相乘?
我们没有任何其他代码来设置它。例如:
request.getSession(false).setMaxInactiveInterval(11);
我们没有这个。但如果我找不到任何解决方案,我会使用它。
例如,对于远程,我更改为:
server.servlet.session.timeout=44s
但我看到的是:
sessionCreated sessionid: 7C3573FE7B5FB6C8939DF8BF60B1B550, setMaxInactiveInterval: 1800, ipaddress: null
Tomcat9是干这个的?
在我的本地,我使用该属性进行测试。
所以
server.servlet.session.timeout=44s
对于我本地的本地和远程服务器数据库配置。
但是这次:
sessionCreated sessionid: 747E6BF3DCD061DFF306325FE4FD76B6, getMaxInactiveInterval: 60, ipaddress: 0:0:0:0:0:0:0:1
747E6BF3DCD061DFF306325FE4FD76B6 0:0:0:0:0:0:0:1 Session Created
我做错了什么?
对于上次测试,我将其添加到本地但具有远程属性的成功处理程序中:
LoggerService.logger.info("onAuthenticationSuccess sessionid: {}, getMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
request.getSession(false).setMaxInactiveInterval(55);
LoggerService.logger.info("onAuthenticationSuccess sessionid: {}, getMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
如果我输入用户名密码,我可以看到:
: onAuthenticationSuccess sessionid: F796EA6C54D8BCA239A36E02C4A7A030, getMaxInactiveInterval: 60, ipaddress: 0:0:0:0:0:0:0:1
: onAuthenticationSuccess sessionid: F796EA6C54D8BCA239A36E02C4A7A030, getMaxInactiveInterval: 55, ipaddress: 0:0:0:0:0:0:0:1
我也是这样做的:
@Bean // Http Listener
public HttpSessionListener httpSessionListener() {
return new HttpSessionListener() {
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
session.setMaxInactiveInterval(55);
LoggerService.logger.info("sessionCreated sessionid: {}, setMaxInactiveInterval: {}, ipaddress: {}",
session.getId(), session.getMaxInactiveInterval(), SecurityUtil.getIpAddress());
又是一样的:
sessionCreated sessionid: FFA7DC9A6558951F1CB790AD9D804F88, getMaxInactiveInterval: 60, ipaddress: null
sessionCreated sessionid: FFA7DC9A6558951F1CB790AD9D804F88, getMaxInactiveInterval: 55, ipaddress: null
FFA7DC9A6558951F1CB790AD9D804F88 0:0:0:0:0:0:0:1 Session Created
对于远程,我使用相同的代码进行了测试并且它也有效但我不想以编程方式设置
sessionCreated before sessionid: 38EC29F7C9C45B34D1FDF05B1F90DC3A, getMaxInactiveInterval: 1800, ipaddress: 192.ss
sessionCreated after sessionid: 38EC29F7C9C45B34D1FDF05B1F90DC3A, getMaxInactiveInterval: 180, ipaddress: 192.ss
所以,有两个问题:
- 为什么 application-remote-properties 超时值不适用于本地?
- 为什么远程超时是10倍(properties有3m但是log显示1800s)
server.*
属性用于控制 Boot 使用的 嵌入式容器。 Spring Boot 将使用 ServletWebServerFactory
实例之一创建 servlet 容器实例。这些 类 使用 server.*
属性来配置受控的 servlet 容器(tomcat、jetty 等)。
但是,当您将应用程序作为 war
文件部署到 Tomcat 实例时,server.*
属性不适用。它们不适用,因为预配置的 servlet 容器可用(因为它是远程 运行 服务)。因此部署到远程 Tomcat 将使 server.*
属性无用。
关于以分钟为单位的会话超时。 Spring开机会convert the session.servlet.session.timeout
property to minutes,所以44s
或55s
会自动转换为1分钟。将它设置为少于一分钟也没有多大意义,因为 Tomcat 每分钟都会使线程 运行 的会话无效。