Scala on Play 2.2:如何在过滤器中重定向页面?

时间:2014-03-21 14:19:40

标签: scala authentication filter playframework-2.2

另外,如果不满足if语句,我如何向用户提供所请求的页面?

这是我到目前为止所做的:

object AuthFilter extends EssentialFilter {
  def apply(next: EssentialAction) = new EssentialAction {
    def apply(request: RequestHeader) = {
      if(request.session.get("auth").isEmpty && !request.path.contains("/login")){
        //redirect to login page.
      } else {
        //show requested page.
        next(request) //is this correct?
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

为什么不延伸Filter而不是EssentialFilter

object AuthFilter extends Filter {

  def apply(next: (RequestHeader) => Future[SimpleResult])(request: RequestHeader): Future[SimpleResult] = {
    if(request.session.get("auth").isEmpty && !request.path.contains("/login")){
        Future.successful(Ok(views.html.yourLoginPage()))
      } else {
        next(request)
      }
  } 
}