2017-05-07 48 views
11

我今天將Play應用程序從2.5升級到2.6,並且ActionBuilder出現問題。文檔狀態:Play 2.6 ActionBuilder

Scala ActionBuilder trait已被修改爲將body的類型指定爲類型參數,並添加抽象解析器成員作爲默認身體解析器。您將需要修改您的ActionBuilder並直接傳遞正文解析器。

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)) 
    } 

} 

有誰知道whic BodyParser我要傳遞的第二個參數?

回答

10

有類似的問題。 Play 2.6會注入一個ControllerComponents,它具有默認的主體解析器。也許這有助於:

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 
} 
0

我得到了同樣的問題。在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 { 
    ... 
    } 
}