如何为 Grails 应用程序配置会话超时?
在我的 Grails 应用程序的一个控制器中,我在会话变量中保留一个参数值,如下所示:
session.myVariable = params.myValue
之后,只要我主动使用该应用程序,我就可以从不同的控制器/GSP 页面访问保存的值。但是,如果我有一段时间不使用我的应用程序,即使我的浏览器窗口仍然打开,会话变量也会失去它的值。
发生这种情况是因为会话过期吗?我的印象是会话一直存在,直到浏览器窗口仍然打开,但显然我错了。
我应该如何确保我在 Grails 应用程序中定义的所有会话变量在浏览器关闭之前不会过期?有没有办法手动设置会话超时?
预先感谢您的回答!
In one of controllers in my Grails application I'm preserving a parameter value in a session variable like this:
session.myVariable = params.myValue
After that, I can access the saved value from different controllers/GSP-pages as long as I actively use the app. However, if I don't use my app for a while, even though my browser window is still open, the session variable looses it's value.
Does this happens because the session expires? I was under impression that a session lives until the browser window is still open, but apparently I was wrong.
What should I do to ensure all session variables I define in my Grails app don't expire until the browser is closed? Is there any way to set session timeout manually?
Thank you in advance for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一种选择是修改 web.xml。之前你必须调用
然后编辑 src/templates/war/web.xml 并在 servlet-mapping 之后添加/修改:
session-timeout 的值以分钟为单位。
Another option would be modifying web.xml. Prior you must call
Then edit src/templates/war/web.xml and add/modify after servlet-mapping:
The value of session-timeout uses minutes as unit.
快进几年...对于 Grails 3.0,使用 ServerProperties 在应用程序配置文件中。
grails-app/conf/application.yml
默认值:1800 秒(30 分钟)
验证超时
HttpSession
使用 getMaxInactiveInterval() 从控制器:
输出 -->
超时:3600 秒
更新 1: 编辑 Grails 3.1 配置更改。
更新 2:对于 Grails 5,请参阅下面 Marc Schmid 的评论。
Fast forward a few years... For Grails 3.0 set the session timeout with ServerProperties in the application configuration file.
grails-app/conf/application.yml
Default value: 1800 seconds (30 minutes)
Verify the timeout for the
HttpSession
from a controller using
getMaxInactiveInterval()
:Output -->
Timeout: 3600 seconds
Update 1: Edited for changes to Grails 3.1 configuration.
Update 2: For Grails 5, see comment below by Marc Schmid.
当前的 grails (2.x) 有一个非常奇怪的设计方法来设置会话超时。流行的想法都不是很好:
注释掉 WebxmlGrails 插件中的“//session Timeout”部分,并将“sessionConfig.sessionTimeout=”添加到 Config.groovy
grails install-templates,从 web.xml 中删除会话超时,在 WebXmlConfig.groovy 中添加超时
等待修复。 :/
一位同事提出了以下代码,该代码对我来说效果很好,直到真正的解决方案内置到 grails 核心中为止。
只需将以下内容添加到 config.groovy 文件的底部,然后设置适当的超时即可。
我建议正确的解决方案是在 Config.groovy 文件中允许一行:
干杯。
The current grails (2.x) have a very odd design approach to setting the session timeout. None of the prevailing ideas are great:
comment out "//session Timeout" section the within the WebxmlGrails Plugin and add "sessionConfig.sessionTimeout=" to Config.groovy
grails install-templates, remove session-timeout from web.xml, add timeout in WebXmlConfig.groovy
wait for a fix. :/
A co-worker came up with the following code that works well for me and will do until a real solution is built into grails core.
Simply add the following to the bottom of your config.groovy file and then set the appropriate timeout.
My I suggest that the correct solution is to allow a single line in the Config.groovy file:
Cheers.
Grails 3.1.x 中不推荐使用会话超时。 application.yml 中的正确属性是:
With Grails 3.1.x session-timeout is deprecated. The correct property in application.yml is:
我可能是错的,但我很确定 Grails 使用与您的应用程序容器关联的会话。例如,如果您使用 Tomcat,则可以指定会话的长度。
更改 Tomcat 会话长度的教程。
I could be wrong, but I'm pretty sure Grails uses the sessions associated with your application container. If you're using Tomcat, for example, you can specify the length of a session.
Tutorial for changing Tomcat session length.
这是一个更好的工作解决方案。进入你的 grails 主目录并找到
示例:E:\grails-2.3.8\src\war\WEB-INF\web3.0.template.xml 将会话超时值编辑为所需值:
示例:
在此处输入代码
90
here is a better working solution. go you your grails home directory and find
Example: E:\grails-2.3.8\src\war\WEB-INF\web3.0.template.xml edit the session time out value to desired values:
Example:
enter code here
90
对于 Grails 3 应用程序,修改
Application.groovy
对我有用:For Grails 3 application, modifying the
Application.groovy
worked for me: