scala - 如何将时间戳添加到我的用户 session 中并设置最长不活动持续时间?

标签 scala playframework playframework-2.0

我在我的应用程序中使用 Play 框架处理 session ,如下面的代码。

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"))
    }

  }

我需要为我的 session 添加超时。在 Play 文档中,

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.).

如何将时间戳添加到我的用户 session 中并设置最大持续时间?

最佳答案

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

application.session.maxAge

session 超时,即 session cookie 的最长期限。如果未设置, session 将在您关闭网络浏览器时过期。例如,将 session 设置为一小时:

application.session.maxAge=1h

记住一周的 session :

application.session.maxAge=7d

默认: session 基于浏览器关闭时过期的临时 cookie。

或者 你可以这样做:

在您登录期间将“最后操作时间”设置为 session 中的当前时间。

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

  • 如果不是,则用户没有 session -> 重定向到静默登录

  • 如果是

    --- 测试最后一次是否超过30分钟

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

关于scala - 如何将时间戳添加到我的用户 session 中并设置最长不活动持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19809764/

相关文章:

postgresql - PlayFramework 2 + Ebean - 原始 Sql 更新查询 - 对数据库没有影响

scala - scala 是否支持多个模式匹配的一个默认子句?

html - 编写媒体查询时出错

playframework - 基于 "resource"连接的客户端正在处理的 BroadcastHub 过滤?

database - 在 application.conf 中为 Play 2.0/anorm 加密数据库密码

Scala:要么,右,左

postgresql - 为什么我的 Play/Heroku/Postgres 部署失败并出现密码错误?

scala - 如何使用自定义 spark-defaults.conf 设置

scala - Scala中抛出异常, "official rule"是什么

java - 在路径中包含 Java 7 的服务器上使用 Play Framework 2.4+