玩吧!框架2.0,使用Security Trait:
如果我让用户浏览未经身份验证的站点的多个部分,但是在他们需要进行身份验证的某些操作上,如何在身份验证之前将他们重定向到其原始URL,而不是将所有URL都重定向到同一URL?
这与Play这个问题的要求类似! 1.x Playframework's Secure module is not redirecting to the original url after login。
但是据我所知,原始URL的flash参数在2.0中不可用。
基本上,我要寻找的更改将在 authenticate 方法处理程序中
def authenticate = Action { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.login(formWithErrors)),
user => Redirect(routes.Application.index).withSession(Security.username -> user._1)
)
}
某种 Redirect(originalRequestUrl)很方便。
有一个干净的解决方案的主意吗?
最佳答案
您可以使用Referer
header ,或在身份验证操作中明确携带返回网址:
使用Referer
header
def authenticate = Action { implicit request =>
loginForm.bindFromRequest.fold(
errors => BadRequest(html.login(errors)),
user => {
val returnUrl = request.headers.get(REFERER).getOrElse(routes.Application.index.url)
Redirect(returnUrl).withSession(Security.username -> user._1)
}
}
明确携带返回网址
def authenticate(returnUrl: String) = Action { implicit request =>
loginForm.bindFromRequest.fold(
errors => BadRequest(html.login(errors, returnUrl)),
user => Redirect(returnUrl).withSession(Security.username -> user._1)
)
}
通过使用
Referer
HTTP header ,您仍然必须处理浏览器未填充此 header 的情况,并且通过将返回URL明确地作为authenticate
操作的参数,您可能会在身份验证代码处理中有更多样板……
关于authentication - 在游戏中!具有安全性的2.0登录后如何重定向到原始URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10215169/