Play Framework:如何将当前请求传递给 body 解析器

Play Framework: How to pass the current request to a body parser

我已经为我的控制器实现了一个 SecureAction,如下所示:

class SecureRequest[A](
  val token: Token,
  request: Request[A]) extends WrappedRequest[A](request) {
  ...
}

class SecureAction[T <: Controller : TypeTag] private(
  private val methodName: String,
  private val tokenTypes: Seq[TokenType]) extends ActionBuilder[SecureRequest] {

  def invokeBlock[A](request: Request[A], block: SecureRequest[A] => Future[Result]) = {
    ...
  }
}

def saveFile = SecureAction.async(fsBodyParser) { implicit request =>

  // code here not executed if current user has not required privileges
  ...
}

一切正常...除了即使请求是 NOT 授权 fsBodyParser 仍然被调用并且多部分文件保存在数据库中。

隐含的 request object 包含安全规则...那么是否可以将请求以某种方式传递给 body 解析器?

编辑

这是我的 body 解析器...

fsBodyParser()(
  implicit fsService: FsServiceComponent#FsService
): BodyParser[MultipartFormData[Future[MetaFile]]] = {
  import BodyParsers.parse._

  multipartFormData(
    Multipart.handleFilePart {
      case Multipart.FileInfo(partName, filename, contentType) =>
        fsService.iteratee(filename, contentType)
    }
  ) 
}

我可能需要一个可以从操作的 body 调用的方法,这样安全性就不会 by-passed。我不清楚 body 签名应该像谁:

def saveFile = SecureAction.async(fsBodyParser) { implicit request =>

  // how should the signature of this method look like???
  parseMultpart(request.body  ???)

  ...
}

BodyParser[T]是一个从RequestHeaderIteratee[Array[Byte], Either[Result, A]]的函数,所以它在执行时确实可以访问请求header。

尽管我会说 Play! 中的 Request => Future[Result] 管道的目的是让 BodyParser 仅转换传入的 body,不对 body 采取行动。我建议您不要将文件存储在 bodyParser 阶段,而是仅在调用 SecureAction 并从请求中检索 body 后才存储。