scala - 玩 2.6 Action 生成器

标签 scala playframework actionbuilder

我今天将我的 Play 应用程序从 2.5 升级到 2.6,但我遇到了 ActionBuilder 的问题。文档状态:

The Scala ActionBuilder trait has been modified to specify the type of the body as a type parameter, and add an abstract parser member as the default body parsers. You will need to modify your ActionBuilders and pass the body parser directly.



documentation

可悲的是我没有找到任何例子,我不知道如何解决这个问题:
class AuthenticatedRequest[A](val token: ProfileTokenData, request: Request[A]) extends WrappedRequest[A](request)

trait Secured {

  object SetExtractor {
    def unapplySeq[T](s: Set[T]): Option[Seq[T]] = Some(s.toSeq)
  }

  def Authenticated = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
      request.jwtSession.claimData.asOpt[JWTToken] match {
        case Some(token) => block(new AuthenticatedRequest(ProfileTokenData(null, token.sub, AuthRole.None), request)).map(_.refreshJwtSession(request))
        case _ => Future.successful(Unauthorized)
      }
    }
  }

  def Registered = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer, AuthRole.Registered))
  }

  def Customer = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin, AuthRole.Customer))
  }

  def Admin = new ActionBuilder[AuthenticatedRequest] with JWTTokenProcess {
    override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] =
      this.processJWTToken(request, block, Seq(AuthRole.Admin))
  }

}

有谁知道我必须通过哪个 BodyParser 作为第二个参数?

最佳答案

有类似的问题。 Play 2.6 注入(inject)了一个 ControllerComponents,它有一个默认的 body 解析器。也许这有帮助:

class CheckApiKey(apiKeyToCheck: String, cc: ControllerComponents)
  extends ActionBuilder[Request, AnyContent] with ActionFilter[Request] {
  ...
  override protected def executionContext: ExecutionContext = cc.executionContext
  override def parser: BodyParser[AnyContent] = cc.parsers.defaultBodyParser
}

关于scala - 玩 2.6 Action 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835032/

相关文章:

scala - 执行reactivemongo聚合框架查询时出现DefaultJSONCommandError问题

php - Google 操作生成器 - 如何从 webhook 请求中读取并避免空响应

scala - 有没有办法在 PersistenceQuery 中使用快照

scala - 理解 Scala 中的 "type arguments do not conform to type parameter bounds"错误

Scala:从泛型到第二泛型的隐式转换

java - 如何有效地与 Scala 中的 Java 流交互?

java - PlayWS - 当请求超时时如何抛出异常?

java - 使用服务器端事件玩 Framework 2.5