Max age is not working in Play Framework 2.2.0 (Scala)

I am using Play Framework 2.2.0 with Scala. I want to expire the session after a fix time, so i put this code in conf/application.conf

application.session.maxAge=1h

but it is not working. Is there any way to set a maximum age for session in application.conf or by overriding any method from controller.If i want to expire session after 50 seconds would i have to code like this

application.session.maxAge=50sec

Thanks for replies

Answers


In Play Framework, you can't set the timeout yourself. As the documentation for the Scala version of the Play Framework states:

There is no technical timeout for the Session. It expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maxmimum inactivity duration, etc.).

So you will have to simply write code for each Action that checks how much time has elapsed between the current time and the creation of the session. I would suggest using a Filter.


The selected answer is outdated now. You can do this by configuring session.maxAge in your application.conf, see the recent pull request.

By default, there is no technical timeout for the Session. It expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maximum inactivity duration, etc.). You can also set the maximum age of the session cookie by configuring the key session.maxAge (in milliseconds) in application.conf.


I had this same problem and added a timestamp (tick) to the session and updated it with each request after checking for a timeout.

Something like this:

// see if the session is expired
String previousTick = session("userTime");
if (previousTick != null && !previousTick.equals("")) {
  long previousT = Long.valueOf(previousTick);
  long currentT = new Date().getTime();
  long timeout = Long.valueOf(Play.application().configuration().getString("sessionTimeout")) * 1000 * 60;
  if ((currentT - previousT) > timeout) {
    // session expired
    session().clear();
    return null;
  } 
}
// update time in session
String tickString = Long.toString(new Date().getTime());
session("userTime", tickString);

http://www.poornerd.com/2014/04/01/how-to-implement-a-session-timeout-in-play-framework-2/

Then add a sessionTimeout=15 (in Minutes) to your conf file.


application.session.maxAge=1d is not working

it seems like a play bug in documentation, but in 2.2.x they simply ommit to mention this option, simply stating that the cookie is transient and expires when the user closes the web browser. (http://www.playframework.com/documentation/2.2.x/ScalaSessionFlash) Note that this is not working, in Chrome I can restart the browser and the cookie is still there and I stay connected. (see chrome://settings/cookies)

the correct option in config file for one day is: session.maxAge=86400 see http://www.racerkidz.com/wiki/Blog:Razie_Div_Blog/Post:No_expiry_for_play_cookie for the source.

PS: I also tried with session.maxAge=1d and the result is no cookie at all and no possible login...


Need Your Help

RDLC expression to display specific records only

c# sql-server rdlc

i have the result from stored procedure and show it on RDLC.