什么是默认会话超时以及在使用 Spring Session 和 Redis 作为后端时如何配置它 [英] What is the default session timeout and how to configure it when using the Spring Session with Redis as the backend

查看:205
本文介绍了什么是默认会话超时以及在使用 Spring Session 和 Redis 作为后端时如何配置它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序目前使用 Spring Session 和 Redis 作为后端.

My application currently use Spring Session together with Redis as the backend.

我搜索了官方文档 用于 Spring Session,但无法找到使用该模块时的默认会话超时时间.

I searched into the official documentation for Spring Session but was not able to find what the default session timeout is when using that module.

我也不确定如何在必要时更改默认超时时间.

Also I am not sure how to change that default timeout if necessary.

有人可以请教吗?

推荐答案

使用 redis 存储库时配置会话超时的最简单方法是

The easiest way to configure session timeout when using redis repository is

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)

OR @EnableRedissonHttpSession(maxInactiveIntervalInSeconds = 1200) 如果存在 redisson 依赖.

OR @EnableRedissonHttpSession(maxInactiveIntervalInSeconds = 1200) if redisson dependency is there.

会话在存储库中不再可用时到期.可以在 RedisOperationsSessionRepositoryMapSessionRepository 上使用 setDefaultMaxInactiveInterval(int) 配置超时.默认值为 30 分钟.

The session expires when it is no longer available in the repository. Timeout can be configured with setDefaultMaxInactiveInterval(int) on both RedisOperationsSessionRepository and MapSessionRepository. Default value is 30 minutes.

如果您使用的是 spring boot,那么从 1.3 版本开始,它会自动将值与应用程序配置中的 server.session.timeout 属性同步.

If you are using spring boot, then as of version 1.3 it will automatically sync the value with the server.session.timeout property from the application configuration.

请注意,使用 spring session 的缺点之一是不会调用 javax.servlet.http.HttpSessionListeners.

Note that one of the shortcomings when using spring session is that javax.servlet.http.HttpSessionListeners are not invoked.

如果您需要对会话过期事件做出反应,您可以订阅您的 spring 应用程序的 SessionDestroyedEvent 应用程序事件.

If you need to react on session expiration events you can subscribe to SessionDestroyedEvent application event of your spring application.

这篇关于什么是默认会话超时以及在使用 Spring Session 和 Redis 作为后端时如何配置它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆