如何在我的用户会话中添加时间戳并设置最大不活动时间? [英] How to add timestamp into my user session and set maximum inactivity duration?

查看:78
本文介绍了如何在我的用户会话中添加时间戳并设置最大不活动时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Play框架处理了会话,如以下代码所示.

I handled session using Play framework in my application like below code.

def login = Action {
    { implicit request =>

          val email = request.body.asFormUrlEncoded.get("email")(0)
          val password = request.body.asFormUrlEncoded.get("password")(0)

          loginForm.bindFromRequest.fold(
              errors => BadRequest(html.login(errors,"Please enter valid username password")),
              //contact => Ok(html.login(loginForm,SignUpProcess.login(email,password)))
              contact => Redirect(routes.Application.home).withSession("email" -> email,"password" -> password)
          )
      }
  }

  def index = Action { request =>
    request.session.get("email").map{ user =>
        Redirect(routes.Application.home).withSession("email" -> user)
    }.getOrElse{
         Ok(views.html.login(loginForm,"Enter username password to login"))
    }

  }

我需要为会话添加超时.在Play文档中,

I need to add timeout for my session. In Play documentation,

该会话没有技术上的超时时间,该超时时间在 用户关闭网络浏览器.如果您需要一个功能超时时间 特定的应用程序,只需将时间戳记存储到用户会话中, 使用它,但是您的应用程序需要使用它(例如,最大会话数 时间,最长不活动时间等).

There is no technical timeout for the session, which 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.).

如何在我的用户会话中添加时间戳并设置最长的持续时间?

How to add timestamp into my user session and set maximum insctivity duration?

推荐答案

通过在conf/application.conf文件中设置配置键的值来在Play应用程序中配置超时.

To Configure timeout in your Play application by setting values for configuration keys in conf/application.conf file.

application.session.maxAge

会话超时,即会话Cookie的最长期限.如果未设置,则在关闭Web浏览器时该会话将过期.例如,将会话设置为一小时:

Session time-out, i.e. the maximum age of the session cookie. If not set, the session expires when you close your web browser. For example, to set the session to one hour:

application.session.maxAge=1h

将会话记住一个星期:

application.session.maxAge=7d

默认:基于临时cookie的会话在浏览器关闭时过期.

Default: the session is based on a transient cookie expires when the browser is closed.

或 您可以执行以下操作:

or You can do something like :

在登录期间,将上次操作时间"设置为会话中的当前时间.

During your login set a "last action time" to current time in the session.

在您的Global类中,如果该标志存在,则覆盖onRequest测试

In your Global class, override onRequest test if that flag exist

  • 如果没有,则用户没有会话->重定向以静默登录

  • if not, user has no session -> redirect to login silently

如果是

---测试上次是否在30分钟前

--- test if last time is more than 30min ago

     ------ if yes, session timeout -> add message to flash, rediect to login   
     ------ if no, update the flag with current time, let the page load

这篇关于如何在我的用户会话中添加时间戳并设置最大不活动时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆