Play 2 中 AuthenticatedBuilder 的异步版本
Posted
技术标签:
【中文标题】Play 2 中 AuthenticatedBuilder 的异步版本【英文标题】:Async version of AuthenticatedBuilder in play 2 【发布时间】:2017-02-22 08:35:31 【问题描述】:我正在尝试使用其他一些操作来组合基本身份验证:
def findByNameSecure(username: String) = Authenticated _ =>
val cursor: Cursor[JsObject] = persons.
find(Json.obj("userdetails.username" -> username)).
cursor[JsObject](ReadPreference.primary)
val res = cursor.collect[List]().map persons =>
Ok(Json.toJson(persons))
.recover
case _ => BadRequest(Json.parse("'error': 'failed to read from db'"))
Await.result(res, 10.seconds)
路线:
GET /secure/user/findbyname controllers.UserController.findByNameSecure(username: String)
这按预期工作。令人不安的是我使用了Await.result
,它正在阻塞。如何编写这种身份验证的异步版本?
我正在使用 play 2.4。
【问题讨论】:
为什么您不编写每个人都可以编译和运行的简单代码示例?你试过了吗 -def findByNameSecure(username: String) = Authenticated.async _ =>
抱歉,我无权展示整个项目。 Authenticated.async
效果很好。谢谢!如果你能把它写成答案,我很乐意接受。
您的业务逻辑与问题无关,因此您可以将其删除。示例:scastie.scala-lang.org/3821 写得越简单,就会得到多少帮助。
【参考方案1】:
AuthendicatedBuilder
是ActionBuilder
的子级。所以我认为它的async
方法应该也可以工作。
使用示例:
def findByNameSecure(username: String) = Authenticated.async _ =>
【讨论】:
以上是关于Play 2 中 AuthenticatedBuilder 的异步版本的主要内容,如果未能解决你的问题,请参考以下文章
如何在 play 2.4 中使用 CommonsMailerPlugin
如何在 Play 的静态方法中使用 play.cache.CacheApi!框架 2.4.2