关于会话:最大年龄在Play Framework 2.2.0(Scala)中不起作用

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

我在Scala中使用Play Framework 2.2.0。我想在修复时间后使会话过期,因此我将此代码放入conf / application.conf

1
application.session.maxAge=1h

但是它不起作用。有没有办法在application.conf中设置会话的最长期限,或者通过覆盖控制器的任何方法。如果我想在50秒后过期会话,我必须像这样

1
application.session.maxAge=50sec

感谢您的回复


所选答案现在已过时。
您可以通过在application.conf中配置session.maxAge来做到这一点,请参阅最近的拉取请求。

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.


在Play框架中,您无法自行设置超时时间。正如Play框架的Scala版本的文档所述:

该会议没有技术上的超时。当用户关闭Web浏览器时,它会过期。如果您需要特定应用程序的功能超时,只需将时间戳记存储到用户Session中并根据您的应用程序的需要使用它(例如,最大会话持续时间,最大非活动持续时间等)。

因此,您将只需要为每个Action编写代码,即可检查从当前时间到创建会话之间经过了多少时间。我建议使用过滤器。


我遇到了同样的问题,并在会话中添加了时间戳(刻度),并在检查超时后用每个请求对其进行了更新。

类似这样的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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/

然后将一个sessionTimeout = 15(以分钟为单位)添加到您的conf文件中。


application.session.maxAge = 1d无法正常工作

这似乎是文档中的一个播放错误,但是在2.2.x中,他们只是忽略提及此选项,只是指出cookie是瞬时的,并且在用户关闭Web浏览器时过期。 (http://www.playframework.com/documentation/2.2.x/ScalaSessionFlash)
请注意,这不起作用,在Chrome浏览器中,我可以重新启动浏览器,并且cookie仍然存在,并且保持连接状态。 (请参阅chrome:// settings / cookies)

一天中配置文件中的正确选项是:
session.maxAge = 86400
参见http://www.racerkidz.com/wiki/Blog:Razie_Div_Blog/Post:No_expiry_for_play_cookie
作为来源。

PS:
我也尝试了session.maxAge = 1d,结果根本没有cookie,也没有可能登录...


现在在playframework中,您可以通过

设置会话到期时间

1
play.http.session.maxAge=Value

在conf文件中。