当我正在进行重定向时,Referer为null

时间:2014-02-06 10:55:39

标签: scala redirect playframework playframework-2.2

我有两页:/home/success/home/error。我希望用户只能通过从其他页面重定向来访问它们,我不认为它们可以直接访问。

# home controller  

def success = Action { r =>
    if (referer.isEmpty) Redirect(routes.Home.index) flashing "error" -> "From home#success: referer was null" // DEBUG
    else Ok(views.html.home.success(request.flash get "success" getOrElse ""))
  }

  def error = Action { implicit request =>
    if (referer.isEmpty) Redirect(routes.Home.index) flashing "error"  -> "From home#error: referer was null" // DEBUG
    else Ok(views.html.home.error(request.flash get "error" getOrElse ""))
  }

到目前为止,这么好 - 它们无法直接从浏览器访问。但是,当我这样做时:

# other controller
def someAction = Action {
  // if everything is ok...
  if (success) Redirect(routes.Home.success) flashing "success" -> "success"

  //else if there is an error
 else Redirect(routes.Home.error) flashing "error" -> "error"
}

它始终使用闪存/index"From home#success: referer was null"将我重定向到"From home#error: referer was null"换句话说,当我重定向到其他页面时,在该页面referer为空

引用者是:

  def referer(implicit request: Request[AnyContent]): Option[String] = request.headers get "referer"

在我的BaseController中。

即使这不起作用:

if (success) Redirect(routes.Home.success) flashing "success" -> "success" withHeaders "referer" -> "something"
else Redirect(routes.Home.error) flashing "error" -> "error" withHeaders "referer" -> "something"

这是怎么回事?

0 个答案:

没有答案