【问题标题】:Async version of AuthenticatedBuilder in play 2Play 2 中 AuthenticatedBuilder 的异步版本
【发布时间】: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


【解决方案1】:

AuthendicatedBuilderActionBuilder 的子级。所以我认为它的async 方法应该也可以工作。

使用示例:

def findByNameSecure(username: String) = Authenticated.async { _ =>

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 2018-06-10
    相关资源
    最近更新 更多