java 使用 Spring Boot 和嵌入式 Tomcat 启用会话持久性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28477199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable session persistence with Spring Boot and embedded Tomcat
提问by
I'm developing an application with Spring Boot and Spring Security by starting the Application class in Eclipse with an embedded Tomcat. Every time I restart the server, my session disappears and I have to log in again which becomes quite annoying.
我正在通过使用嵌入式 Tomcat 在 Eclipse 中启动 Application 类来使用 Spring Boot 和 Spring Security 开发应用程序。每次我重新启动服务器时,我的会话都会消失,我必须再次登录,这变得很烦人。
Is it possible to persist the sessions between server restarts?
是否可以在服务器重新启动之间保持会话?
I saw this other question on Stackoverflow where someone asks the opposite, which makes me think that this should actually work out-of-the-box:
我在 Stackoverflow 上看到了另一个问题,有人提出了相反的问题,这让我认为这实际上应该是开箱即用的:
How to disable Tomact session persistence in Spring Boot via Manager pathname?
如何通过管理器路径名在 Spring Boot 中禁用 Tomact 会话持久性?
I'm running Spring Boot 1.2.1 with Gradle.
我正在使用 Gradle 运行 Spring Boot 1.2.1。
btw, I know about Spring Loaded, but sometimes a server restart is unavoidable.
顺便说一句,我知道 Spring Loaded,但有时服务器重启是不可避免的。
采纳答案by Faraj Farook
I just figured this out myself. Everytime the application is started, Spring generates a new random temporary directory in /tmp
for Tomcat's base directory (e.g. /tmp/tomcat.5990562997404648887.8080
). Since it uses a different folder on each start, Tomcat has no way to restore the session.
我只是自己想通了这一点。每次应用程序启动时,Spring 都会在/tmp
Tomcat 的基本目录(例如/tmp/tomcat.5990562997404648887.8080
)中生成一个新的随机临时目录。由于每次启动时使用不同的文件夹,Tomcat 无法恢复会话。
This can be worked around by setting your own base directory with server.tomcat.basedir=/tmp
. However, I don't consider this a fix since it requires setting an operating system specific directory, so I opened a bug about this: https://github.com/spring-projects/spring-boot/issues/2490
这可以通过使用server.tomcat.basedir=/tmp
. 但是,我不认为这是一个修复,因为它需要设置操作系统特定的目录,所以我打开了一个关于这个的错误:https: //github.com/spring-projects/spring-boot/issues/2490
回答by Faraj Farook
According to the Spring this will be fixed in 1.3.0.M2and eventually in 1.3.0.RELEASE
根据 Spring,这将在1.3.0.M2 中修复,并最终在1.3.0.RELEASE中修复
Then all you got to do is add the following line to your application.properties
file.
然后您要做的就是将以下行添加到您的application.properties
文件中。
server.session.persistent=true
In recent Spring versions this has been deprecated and replaced by:
在最近的 Spring 版本中,这已被弃用并替换为:
server.servlet.session.persistent=true
Reference https://github.com/spring-projects/spring-boot/issues/2490
参考https://github.com/spring-projects/spring-boot/issues/2490
Update Tomcat, Jetty and Undertow to serialize session data when the application is stopped and load it again when the application restarts.
Persistent session are opt-in; either by setting
persistentSession
on the ConfigurableEmbeddedServletContainer or by using the propertyserver.session.persistent=true
.Fixes gh-2490
更新 Tomcat、Jetty 和 Undertow 以在应用程序停止时序列化会话数据,并在应用程序重新启动时再次加载它。
持久会话是可选的;通过
persistentSession
在 ConfigurableEmbeddedServletContainer 上设置或使用属性server.session.persistent=true
。修复 gh-2490
回答by Alexander
I solved it by using Redis to persist sessions info.
我通过使用 Redis 来保存会话信息来解决它。
All you need to do is specify a few options in application.yml file:
您需要做的就是在 application.yml 文件中指定几个选项:
server:
servlet:
session:
persistent: true
spring:
session:
store-type: redis
redis:
host: localhost
port: 6379
...
build.gradle
构建.gradle
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
id 'org.springframework.boot' version '2.1.3.RELEASE'
}
...
// Spring Framework
compile(
'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-data-jpa',
'org.springframework.data:spring-data-redis',
'org.springframework.boot:spring-boot-starter-security'
)
...
Works perfect with Spring Boot 2.1.3
与 Spring Boot 2.1.3 完美配合