在新特性中实现WithFilters

时间:2014-09-03 22:38:28

标签: scala playframework

this article的“使用过滤器”部分介绍了如何在Play Framework Global对象上实现过滤器。

object Global extends WithFilter(AccessLog)

object AccessLog extends Filter {
  override def apply(next: RequestHeader => Future[Result])(request: RequestHeader): Future[Result] = {
    val result = next(request)
    play.Logger.info(request + "\n\t => " + result)
    result
  }
}   

(see framework source code here - line 93)

我正在创建自己的特性,扩展了GlobalSettings,而在doFilter方法中,我希望访问Filter.apply()可用的请求参数,但GlobalSettings的方法声明如此

override def doFilter(next: RequestHeader => Handler): (RequestHeader => Handler) = {
  play.Logger.debug("doFilter:")
  super.doFilter(next)
}

在覆盖上述方法时如何获取RequestHeader参数?

1 个答案:

答案 0 :(得分:0)

doFilter方法有两个参数集,第二个集只包含一个隐式请求,因此必须将其推送到重写方法中,如此...

  override def doFilter(next: RequestHeader => Handler): (RequestHeader => Handler) = {
    request => {
      play.Logger.debug("doFilter: " + request)
      super.doFilter(next)(request)
    }
  }