@EnableRedisHttpSession + Spring Boot忽略application.yml上的server.session.timeout

问题描述 投票:8回答:5

我有一个Spring Boot 1.3.3 [另一个东西]的项目,Redis可以配置来管理会话,即@EnableRedisHttpSession。该应用程序运行良好,并定期将信息存储在Redis上。我面临的问题是,与文档所说的不同,无论我是否定义了server.session.timeout,Redis总是使用其注释属性的默认值(maxInactiveIntervalInSeconds),即:1800

在这里,我遵循的文档:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-session.html

我也尝试过@rwinch在https://github.com/spring-projects/spring-session/issues/110定义的方法,但也没有成功。

更新......


我的配置文件请求:

#First attempt (server.session.timeout) following the Spring documentation mentioned
server:
   session:
     timeout: 10  
spring:
   #session timeout under spring (as mentioned by M Deinum in comment - unfortunately doesnt work)
   session:
     timeout: 10
   redis:
     host: 192.168.99.101
     port: 6379

除此之外,我还尝试实现一个负责设置超时的SessionListener(类似这样):

    public class SessionListener implements HttpSessionListener {
        @Value(value = "${server.session.timeout}")
        private int timeout;
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            if(event!=null && event.getSession()!=null){
                event.getSession().setMaxInactiveInterval(timeout);
            }
        }
...

它仍然没有导致正确的情况。我真的在绞尽脑汁:|


拜托,伙计们,我错过了一些观点吗?有没有其他人面对过它?

提前致谢。

spring-boot spring-session
5个回答
4
投票

好吧,万一有人面临同样的情况,我们有2种解决方法:

I.实施以下内容:

@EnableRedisHttpSession
public class Application {
 //some other codes here
    @Value("${spring.session.timeout}")
    private Integer maxInactiveIntervalInSeconds;
    @Bean
    public RedisOperationsSessionRepository sessionRepository( RedisConnectionFactory factory) {
        RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository(factory);
        sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
        return sessionRepository;
    }

不幸的是,我必须实现一个监听器,以便在会话到期时执行其他操作。并且,当您定义RedisOperationsSessionRepository时,您不再拥有HttpSessionListener(而不是它,您有一个SessionMessageListener,如下所述:http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository)。由于这个问题,第二种方法是必需的。

II。要克服这个问题:

@EnableRedisHttpSession
public class Application implements ApplicationListener{

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

    @Autowired
    private RedisOperationsSessionRepository redisOperation;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            redisOperation.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
        }
    }
    ...

假设它们都不是理想的开箱即用设置,至少它们允许我继续使用我的PoC。


4
投票

另一种方案:

@EnableRedisHttpSession
public class HttpSessionConfig {

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

    @Inject
    private RedisOperationsSessionRepository sessionRepository;

    @PostConstruct
    private void afterPropertiesSet() {
        sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInMinutes * 60);
    }

这样您就可以使用默认配置,只需添加超时即可。因此,您维护默认的HttpSessionListener,并且您不需要使用ApplicationListener来设置应用程序生命周期中的超时时间。


1
投票
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)

1
投票

扩展RedisHttpSessionConfiguration并使用@PostConstruct方法执行init。

@Configuration
public class HttpSessionConfig extends RedisHttpSessionConfiguration {

  @Value("${spring.session.timeout}")
  private Integer sessionTimeoutInSec;
  @Value("${spring.session.redis.namespace}")
  private String sessionRedisNamespace;

  @Bean
  public LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }

  @PostConstruct
  public void initConfig() throws Exception {
    this.setMaxInactiveIntervalInSeconds(sessionTimeoutInSec);
    this.setRedisNamespace(sessionRedisNamespace);
  }
}

1
投票

您可以删除EnableRedisHttpSession注释,而是设置属性:

spring.session.store-type=redis

spring.session.timeoutserver.servlet.session.timeout都可以使用。请注意spring.session.timeout将根据我的测试覆盖server.servlet.session.timeout

© www.soinside.com 2019 - 2024. All rights reserved.