Parsing Play 2.0框架,使用带身份验证请求的BodyParser

Parsing Play 2.0框架,使用带身份验证请求的BodyParser,parsing,scala,playframework,Parsing,Scala,Playframework,我希望能够在经过身份验证的请求上使用BodyParser,但如果像Zentarks示例那样设置身份验证,我很难弄清楚如何做到这一点 我的认证方法 def IsAuthenticated(f: => String => Request[AnyContent] => Result) = { Security.Authenticated(username, onUnauthorized) { user => Action(request => f(user)(

我希望能够在经过身份验证的请求上使用BodyParser,但如果像Zentarks示例那样设置身份验证,我很难弄清楚如何做到这一点

我的认证方法

def IsAuthenticated(f: => String => Request[AnyContent] => Result) = {
  Security.Authenticated(username, onUnauthorized) { user =>
    Action(request => f(user)(request))
  }
}

def HasRole(role: List[String])
  (f: => String => Request[AnyContent] => Result) = IsAuthenticated {
  user => request => if (role.contains(getRole(user))) {
    f(user)(request) // This function returns the result.
  } else {
    Results.Forbidden
  }
}
我的控制器方法

def controller = HasRole(List("admin")) { user => _ => { 
  Action(parse.temporaryFile){ implicit request =>
    request.body.moveTo(new File("/tmp/filepath"))
    Redirect(routes.home)
  }
}
这就是我看到的错误

[error]  found   : play.api.mvc.Action[play.api.libs.Files.TemporaryFile]
[error]  required: play.api.mvc.Result
[error]       Action(parse.temporaryFile){ implicit request =>
[error]                                  ^
这是一个相关的问题:


此人找到了一个解决方法,我相信临时文件示例也有一个解决方法,但我想知道我正在做的事情是如何(或为什么)不起作用的。

我相信我已经解决了这个问题,主要是因为我在最初的问题中留下了一些细节,我没有意识到这些细节是重要的

问题是我正在包装一个
Action{Action{}
,因为
IsAuthenticated
方法中已经有一个对
Action
函数的调用。我最后做的是用一个将
BodyParser
作为参数的方法重载
IsAuthenticated
函数。因为我使用的是
TemporaryFile
方法,它不是
AnyContent
的子类,所以我还必须更改请求类型

现在,这就是我的
Secured
trait看起来的样子:

def IsAuthenticated(f: => String => Request[Any] => Result) = {
  Security.Authenticated(username, onUnauthorized) { user =>
    Action(request => f(user)(request))
  }
}

def IsAuthenticated(b: BodyParser[Any] = parse.anyContent)
  (f: => String => Request[Any] => Result) = {
  Security.Authenticated(username, onUnauthorized) { user =>
    Action(b)(request => f(user)(request))
  }
}

def HasRole(role: List[String])(b: BodyParser[Any] = parse.anyContent)
  (f: => String => Request[Any] => Result) = IsAuthenticated(b) {
  user => request => getRole(user) match {
    case Some(r) if role.contains(r) => f(user)(request)
    case _ => Results.Forbidden
  }
}
def controller = HasRole(List("admin"))(parse.temporaryFile) { user => request =>
  request.body match {
    case b:TemporaryFile => b.moveTo(new File("/tmp/file"))
    case _ => Status(404)
  }
}
这就是我的控制器的外观:

def IsAuthenticated(f: => String => Request[Any] => Result) = {
  Security.Authenticated(username, onUnauthorized) { user =>
    Action(request => f(user)(request))
  }
}

def IsAuthenticated(b: BodyParser[Any] = parse.anyContent)
  (f: => String => Request[Any] => Result) = {
  Security.Authenticated(username, onUnauthorized) { user =>
    Action(b)(request => f(user)(request))
  }
}

def HasRole(role: List[String])(b: BodyParser[Any] = parse.anyContent)
  (f: => String => Request[Any] => Result) = IsAuthenticated(b) {
  user => request => getRole(user) match {
    case Some(r) if role.contains(r) => f(user)(request)
    case _ => Results.Forbidden
  }
}
def controller = HasRole(List("admin"))(parse.temporaryFile) { user => request =>
  request.body match {
    case b:TemporaryFile => b.moveTo(new File("/tmp/file"))
    case _ => Status(404)
  }
}
希望这对其他人有帮助