How to set different session timeout for each application?

As we learnt in our previous article How to set Session timeout in Hybris using below key value pair

  1. Default.session.timeout=1000
Default.session.timeout=1000


This timeout value is a global timeout value which means its applicable for all the hybris applications like storefront,cockpit,hmc etc.

Sometimes its not required to keep same timeout value for all the applications.

In such case, Hybris provides a way to define application specific timeout

We can achieve this by providing a custom sessionCloseStrategy Spring bean in the web Spring context

This strategy class has to either implement the SessionCloseStrategy interface or can extend the DefaultSessionCloseStrategy class

We can define this strategy in the extension where we need to set different session timeout

Let’s see how we can do it for storefront

Step 1

First create a class which extends DefaultSessionCloseStrategy

  1. public class StorefrontSessionCloseStrategy extends DefaultSessionCloseStrategy
  2. {
  3.     @Override
  4.     public void setTimeoutOnHttpSessionCreation(final HttpSession httpSession)
  5.     {
  6.         int timeoutSeconds = 3000; //Load this value from property file
  7.         httpSession.setMaxInactiveInterval(timeoutSeconds);
  8.     }
  9. }
public class StorefrontSessionCloseStrategy extends DefaultSessionCloseStrategy
{
    @Override
    public void setTimeoutOnHttpSessionCreation(final HttpSession httpSession)
    {
        int timeoutSeconds = 3000; //Load this value from property file
        httpSession.setMaxInactiveInterval(timeoutSeconds);
    }
}

Step 2

Register the above class as a spring bean in the storefront web application context

  1. <alias alias="sessionCloseStrategy" name="storefrontSessionCloseStrategy"/>
  2. <bean id="storefrontSessionCloseStrategy" class=“com.kb.storefront.StorefrontSessionCloseStrategy"/>
<alias alias="sessionCloseStrategy" name="storefrontSessionCloseStrategy"/>
<bean id="storefrontSessionCloseStrategy" class=“com.kb.storefront.StorefrontSessionCloseStrategy"/>


Note :

Bean alias must exactly be sessionCloseStrategy in order for the bean to be picked up by hybris

Now default.session.timeout value is not applicable for storefront web application, instead value we set in this strategy will be used.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on