Play 2.0 框架,使用带有经过身份验证的请求的 BodyParser [英] Play 2.0 Framework, using a BodyParser with an authenticated request
问题描述
我希望能够对经过身份验证的请求使用 BodyParser,如果我的身份验证设置为 ZenTasks 示例,我无法弄清楚如何执行此操作.
I'd like to be able to use a BodyParser on an authenticated request and I'm having trouble figuring out how to do that if my Authentication is set up like the ZenTasks example.
我的身份验证方法,
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] ^
这是一个相关的问题:已验证播放请求的 parse.json
此人找到了一个解决方法,我相信临时文件示例也有一个解决方法,但我想知道我正在做什么(或为什么)不起作用.
This person found a workaround, and I believe there is one for the temporary file example as well, but I'd like to know how (or why) what I'm doing is not working.
推荐答案
我相信我已经想通了,主要是因为我在原始问题中遗漏了一些我没有意识到很重要的细节.
I believe I've figured this out, mainly because I left some details out of the original question that I did not realize were important.
问题是我包装了一个 Action { Action { } }
因为 IsAuthenticated
方法已经调用了 Action
函数在里面.我最终做的是使用一个将 BodyParser
作为参数的方法重载 IsAuthenticated
函数.因为我使用的是 TemporaryFile
方法,它不是 AnyContent
的子类,所以我还必须更改请求类型.
The problem was that I was wrapping an Action { Action { } }
because the IsAuthenticated
method already had a call to the Action
function inside it. What I ended up doing was overloading the IsAuthenticated
function with a method that took BodyParser
as a parameter. Because I am using the TemporaryFile
method, which is not a subclass of AnyContent
, I also had to change the request type.
现在,这就是我的 Secured
特性的样子:
Now, this is what my Secured
trait looks like:
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
}
}
这就是我的控制器的样子:
And this is what my controller looks like:
def controller = HasRole(List("admin"))(parse.temporaryFile) { user => request =>
request.body match {
case b:TemporaryFile => b.moveTo(new File("/tmp/file"))
case _ => Status(404)
}
}
希望这对其他人有帮助!
Hope this helps someone else!
这篇关于Play 2.0 框架,使用带有经过身份验证的请求的 BodyParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!