在玩!2.0具有安全特性如何在登录后重定向到原始URL?

flu*_*rdy 6 authentication playframework-2.0

随着玩!框架2.0,使用安全特征:

如果我让用户浏览到未经身份验证的网站的多个部分,但是在某些操作上需要进行身份验证,如何在身份验证之前将它们重定向到原始网址,而不是所有人都使用相同的网址?

Play的这个问题也有类似的要求!1.x Playframework的安全模块在登录后没有重定向到原始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)
    )
  }
Run Code Online (Sandbox Code Playgroud)

某种Redirect(originalRequestUrl)会很方便.

有关清洁解决方案的任何想法?

Jul*_*Foy 11

您可以使用Referer标头,或在验证操作中明确地携带返回URL:

使用Referer标题

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

明确地携带返回URL

def authenticate(returnUrl: String) = Action { implicit request =>
  loginForm.bindFromRequest.fold(
    errors => BadRequest(html.login(errors, returnUrl)),
    user => Redirect(returnUrl).withSession(Security.username -> user._1)
  )
}
Run Code Online (Sandbox Code Playgroud)

通过使用RefererHTTP标头,您仍然必须处理浏览器未填充此标头的情况,并通过明确地将返回URL作为您的authenticate操作的参数,您可能在代码处理中使用更多样板进行身份验证...