自动用户登录并将用户信息保存在 Play Framework 中的好方法

Good way to automate user login and keep the user information in Play Framework

首先,我还是scala的新手,所以这个问题看起来很基础,或者我的方法不对,但我不知道该怎么做:

我已经将 Oauth2 身份验证集成到我的项目中,现在我正在尝试使登录自动化,而不是在每个函数中编写:

Action.async { implicit request =>
        authorize(new OauthDataHandler()) { authInfo =>

代码,我想使用我的自定义操作并执行:

def index = OAuth2Action { implicit request =>
      Future(Ok("Welcome"))
}

object OAuth2Action extends OAuth2Provider{

    def apply(f: Request[AnyContent]  => Future[Result]): Action[AnyContent] = {
      Action.async { implicit request =>
        authorize(new OauthDataHandler()) { authInfo =>
          f(request)
        }
      }
    }
}

但是,这里是我缺少 scala 语法的地方,我想保留我的 authInfo 对象的引用,以便在 index 函数中使用(因为我需要一些那里的用户信息)。有什么办法吗?

提前致谢

编辑: 基于@johanandren 回答的解决方案,我意识到我无法通过 Actions 发送此数据,所以我使用 ActionBuilder 进行身份验证:

case class AuthenticatedRequest[A](user: User, request: Request[A]) extends WrappedRequest(request)

object Authenticated extends ActionBuilder[AuthenticatedRequest] with OAuth2Provider {

  def invokeBlock[A](request: Request[A], block: AuthenticatedRequest[A] => Future[Result]) = {
    authenticate(block)(request)
  }

  def authenticate[A]( block: AuthenticatedRequest[A] => Future[Result])(implicit request: Request[A]) = {
    authorize(new OauthDataHandler()) { authInfo =>
      block(AuthenticatedRequest(authInfo.user, request))
    }
  }
}

在游戏文档中查看动作构建器的动作组合,完全按照您的意愿行事:

https://www.playframework.com/documentation/2.3.x/ScalaActionsComposition