15

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

Scala ActionBuilder trait 已被修改为将主体的类型指定为类型参数,并添加一个抽象解析器成员作为默认的主体解析器。您将需要修改您的 ActionBuilders 并直接传递正文解析器。

文件

可悲的是我没有找到任何例子,我不知道如何解决这个问题:

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 作为第二个参数?

4

2 回答 2

15

有类似的问题。Play 2.6 注入了一个 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
}
于 2017-05-16T09:36:05.457 回答
3

我遇到了同样的问题。在 Play 2.6.x 中,您需要注入 BodyParser 并扩展 ActionBuilder。你也可以这样试试

     class SecuredActionBuilder(val parser: BodyParser[AnyContent])(implicit val executionContext: ExecutionContext) extends ActionBuilder[SecuredRequest, AnyContent] {
 def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
  request.jwtSession.claimData.asOpt[JWTToken] match {
   ...
  }
}
于 2017-08-29T11:50:01.843 回答