How to configure a session timeout for Grails application?

39,125

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

server:
    session.timeout: 7200

Solution 5

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.

Share:
39,125
curd0
Author by

curd0

Updated on July 08, 2022

Comments

  • curd0
    curd0 almost 2 years

    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!