【问题标题】:Springboot app session timeoutSpring Boot 应用会话超时
【发布时间】:2015-05-21 14:30:21
【问题描述】:

我创建了一个 SpringBoot MVC/Security 应用程序 1.2.2.RELEASE 并且我的 application.properties 包含服务器设置,如

#Tomcat port and contextPath details
server.port=8080
server.contextPath=/test
#server.session-timeout=120
server.sessionTimeout=120

documentation 状态

server.session-timeout= # session timeout in seconds

ServerProperties.java 使用 sessionTimeout;

如果您查看我提出的 application.properties 代码,我已经独立尝试和一起尝试过,但 2 分钟后我没有超时,我没有明确编写任何其他代码来执行任何会话处理。

有人遇到过这个问题吗?我错过了什么或做错了什么?

【问题讨论】:

  • Boot 的relaxed binding 表示server.sessionTimeoutserver.session-timeout 都将配置ServerProperies'sessionTimeout 属性。请注意,单位是秒,而不是分钟。
  • @Andy,感谢您提供的信息,但这仍然不能解释为什么我没有超时,即使我将值设置为 120 秒(2 分钟)
  • 这就是为什么它是评论而不是答案
  • 据此,超时不以分钟数表示:stackoverflow.com/questions/24561915/…
  • 为什么会超时?会话将被清理,如果您将其与 Spring Security 混合(从您的问题中看不出来),则可能是配置错误。此外,超时约为 2 分钟,具体取决于 reaper 线程何时运行,而不是 2 分钟,实际上可能是 3 分钟,具体取决于清理会话的线程。

标签: java spring-boot session-timeout


【解决方案1】:

我不知道出于某种原因只设置了

server.session.timeout=120 

但是,当我同时设置会话超时和 cookie 最大年龄时,它对我不起作用:

server.session.cookie.max-age=120
server.session.timeout=120 

效果很好

【讨论】:

  • 之所以有效,是因为浏览器使cookie无效并且不将其发送到服务器,因此服务器找不到会话
  • 这样,无论活动如何,cookie都会失效,因此,您在填写表单时可能会被注销,而在您提交时,您将被重定向。
【解决方案2】:

我不确定这个 server.session.timeout 是做什么用的,因为当我将它设置为特定数字并监控会话创建时,会话到期不会改变。

我正在使用 spring session 和 redis 集成,就我而言,我需要将 maxInactiveIntervalInSeconds 设置为 120(秒),这可以通过 redisHttpSessionConfiguration 完成。

然后如果我去 redis 查找会话,我可以看到它的到期时间更改为 120 秒并且会话超时有效。

我的一个建议是尝试找出是否可以通过编程方式或在属性文件中配置会话的 maxInactiveIntervalInSeconds(或类似)并监控会话更改。

【讨论】:

    【解决方案3】:

    (在撰写本文时这适用于 Spring 1.5.x)

    请注意,如果您使用的是 Redis session @EnableRedisHttpSession(例如在其他评论中 @Phoebe Li's case),则不会应用应用程序属性 server.session。您必须通过如下代码手动设置:

    @EnableRedisHttpSession
    public class HttpSessionConfig {
        @Bean
        public RedisOperationsSessionRepository sessionRepository(RedisConnectionFactory factory) {
            RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(factory);
    
            //Set the TTL of redis' key, which in turn will expire session when TTL is reached
            sessionRepository.setDefaultMaxInactiveInterval(15); //e.g. 15 seconds
    
            return sessionRepository;
        }I
    }
    

    【讨论】:

      【解决方案4】:

      在我的 Spring Boot 2 应用程序的 application.yml 中

      # A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits
      server:
        servlet:
          session:
            cookie:
              max-age: -1
            timeout: -1
      

      通过这些设置JSESSIONID cookie 过期时间设置为“浏览会话结束时”。

      【讨论】:

        【解决方案5】:

        您可以尝试添加这两个语句。

        server.session.cookie.max-age=120
        server.session.timeout=120
        

        你可以在我的博客上找到完整的例子:http://www.onlinetutorialspoint.com/spring-boot/how-to-set-spring-boot-tomcat-session-timeout.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-14
          • 2017-04-19
          • 2016-09-15
          • 2015-12-10
          • 2017-03-16
          • 1970-01-01
          • 2019-03-10
          • 2019-08-08
          相关资源
          最近更新 更多