播放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
Here is a related question: parse.json of authenticated play request
此人找到了一种解决方法,我认为也有一个用于临时文件的示例,但是我想知道我正在做的事情(或为什么)不起作用.
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
函数.因为我使用的不是AnyContent
的子类的TemporaryFile
方法,所以我还必须更改请求类型.
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!
这篇关于播放2.0框架,将BodyParser与经过身份验证的请求一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!