And*_*usa 8 scala playframework-2.0
我真的很喜欢在用户浏览器上保存会话数据,但不喜欢会话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
}
}
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
每5分钟生产一次饼干:
The cookies produced every 5min:
Cookie:PS="a6bdf9df798c24a8836c2b2222ec1ea4a4251f301-username=admin&ts=1381180064"
Cookie:PS="D7edg7df709b54B1537c2b9862dc2eaff40001c90-username=admin&ts=1381180380"
Run Code Online (Sandbox Code Playgroud)