![](/img/trans.png)
[英]Play Framework version 2.6/2.7 set PLAY_SESSION cookie to SameSite=None; Secure
[英]Better ways to implement more secure Play Scala framework session via cookie
我真的很喜欢将会话数据保存在用户浏览器中的想法,但是不喜欢会话cookie在play框架中不是很安全的事实。 如果有人窃取了Cookie,则他/她可以使用它来永久访问站点,因为cookie签名没有到期,并且cookie到期在这里没有帮助,因为如果有人偷了cookie,它就不会停止重用cookie。
如果用户仍在使用该站点,则我添加了时间戳以使会话在1小时后过期,并且每5分钟更新一次时间戳,以便用户仍在使用该站点,因此cookie签名将滚动并到期。
我对scala和play框架很陌生,因此,任何建议或更好的方法都可以实现这一点。
trait Secured {
def withAuth(f: => String => Request[AnyContent] => Result) = {
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => {
val sessionRolloverPeriod = 300
val sessionExpiryTime = 3600
val sessionCreationTime: Int = request.session("ts").toInt
val currentTime = System.currentTimeMillis() / 1000L
if(currentTime <= (sessionCreationTime + sessionExpiryTime)) {
if(currentTime >= (sessionCreationTime + sessionRolloverPeriod)) {
f(user)(request).withSession(request.session + ("ts" -> (System.currentTimeMillis() / 1000L).toString))
} else {
f(user)(request)
}
} else {
Results.Redirect(routes.Auth.login()).withNewSession
}
}
)
}
}
}
每5分钟产生一次Cookie:
The cookies produced every 5min:
Cookie:PS="a6bdf9df798c24a8836c2b2222ec1ea4a4251f301-username=admin&ts=1381180064"
Cookie:PS="D7edg7df709b54B1537c2b9862dc2eaff40001c90-username=admin&ts=1381180380"
对我来说似乎合理,不过我可能将其放在服务器端,为客户端提供“会话ID”,并在用户注销时删除该会话。 在客户端执行所有操作意味着,如果会话已被盗,则除了等待超时外,无法使会话无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.