How to configure a session timeout for Grails application?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 17:41:12

Another option would be modifying web.xml. Prior you must call

grails install-templates

Then edit src/templates/war/web.xml and add/modify after servlet-mapping:

<session-config>
   <session-timeout>60</session-timeout>
</session-config>

The value of session-timeout uses minutes as unit.

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

server:
   session:
      timeout: 3600  #seconds

Default value: 1800 seconds (30 minutes)

Verify the timeout for the HttpSession from a controller using getMaxInactiveInterval():

log.println "Timeout: ${session.getMaxInactiveInterval()} seconds"

Output --> Timeout: 3600 seconds

Update: Edited configuration for changes in Grails 3.1

Lord Voldemort

The current grails (2.x) have a very odd design approach to setting the session timeout. None of the prevailing ideas are great:

  1. comment out "//session Timeout" section the within the WebxmlGrails Plugin and add "sessionConfig.sessionTimeout=" to Config.groovy

  2. grails install-templates, remove session-timeout from web.xml, add timeout in WebXmlConfig.groovy

  3. 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.

grails.war.resources = { stagingDir, args ->
  def webXML = new java.io.File("${stagingDir}/WEB-INF/web.xml")
  webXML.text = webXML.text.replaceFirst("<session-timeout>30</session-timeout>", "<session-timeout>90</session-timeout>")
}

My I suggest that the correct solution is to allow a single line in the Config.groovy file:

session.timeout = 90;

Cheers.

With Grails 3.1.x session-timeout is deprecated. The correct property in application.yml is:

server:
    session.timeout: 7200

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.

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

For Grails 3 application, modifying the Application.groovy worked for me:

package foo

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.apache.catalina.Context
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
import org.springframework.context.annotation.Bean

class Application extends GrailsAutoConfiguration {

    static void main(String[] args) {
        GrailsApp.run(Application, args)
    }

    @Bean
    EmbeddedServletContainerFactory containerFactory() {
        TomcatEmbeddedServletContainerFactory containerFactory = new TomcatEmbeddedServletContainerFactory()

        containerFactory.addContextCustomizers(new TomcatContextCustomizer() {
            @Override
            void customize(Context context) {
                int oneWeekInMinute = 7 * 24 * 60
                context.setSessionTimeout(oneWeekInMinute)
            }
        });

        return containerFactory
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!