server.session.timeout in application.yml is not used when deployed to a tomcat as a war

7.7k Views Asked by At

I have this configuration in my application.yml

server:
    contextPath: /appname
    session:
        timeout: 7200  # 2 hours in seconds

This works fine when i run in Intellij IDEA, but when i deploy the build .war file to a tomcat instance this is ignored. Is this a bug or is it not expected to work like this?

Also i seem to be unable to locate a specification of what can be written in application.yml. Anyone know where this can be found?

How about the application.groovy config file? Cant seem to locate a specification for this?

My environment:

  • Grails version: 3.2.8
  • Gradle version: 3.4.1
  • Intellij IDEA version: 2017.1.2
  • Tomcat version: 8.0.26 JDK Version: 1.8.0_45
3

There are 3 best solutions below

0
Sergio del Amo On BEST ANSWER

When you deploy a Grails 3 app to a standalone tomcat application you should not use springboot server.session.timeout configuration property. That it is only for an embeedded server.

Spring boots - sever.session.timeout - Embedded Server configuration

To configure a session timeout in a SpringBoot app (Grails 3 app is built on top of SpringBoot app) deployed into a standalone tomcat you have two choices:

A) Timeout for every app deployed in that tomcat instance.

You could edit the session timeout directly in tomcat configuration files:

$TOMCAT_HOME/conf/web.xml

Look out for the block:

<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly   -->
<!-- created sessions by modifying the value below.   -->

 <session-config>
     <session-timeout>30</session-timeout>
 </session-config>

B) You can add a web.xml file in your Grails 3 app, with the timeout you need per app.

Create a file in the path 'src/main/webapp/WEB-INF/web.xml' with the content:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <session-config>
     <session-timeout>30</session-timeout>
 </session-config>
</web-app>
0
Tom Supergan On

C) You can also use tomcat's HttpSession setMaxInactiveInterval(seconds) method to set in your Groovy code.

if (grailsApplication.config.getProperty("session.timeout")?.isInteger())
    // session timeout in seconds
    session.setMaxInactiveInterval(grailsApplication.config.session.timeout as int)
0
Radim On

Note that with the (current latest) Grails 5.x and spring boot 2.5 the correct property name is server.servlet.session.timeout and hence the application.yml config would go like this:

server:   
    servlet:
      session:
        timeout: 3600  #seconds

Spring boot docs: https://docs.spring.io/spring-boot/docs/2.5.5/reference/html/application-properties.html#application-properties.server.server.servlet.session.timeout