我在我的应用程序中使用 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/