【发布时间】: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 写得越简单,就会得到多少帮助。
标签: scala asynchronous playframework-2.4