Cognito用户池:在aws cognito java sdk中,如何在accessToken过期后使用refreshToken获取新的accessToken?

问题描述 投票:0回答:1

我正在基于Scala Play框架的Web应用程序中使用AWS Cognito作为用户管理解决方案。我正在使用以下代码登录。

var mIdentityProvider: AWSCognitoIdentityProvider = getAmazonCognitoIdentityClient;

def sessionLogin(userName: String, password: String): AdminInitiateAuthResult = {
val authParams: java.util.Map[String, String] = new java.util.HashMap[String, String]()
    authParams.put("USERNAME", userName)
    authParams.put("PASSWORD", password)
    val authRequest = new AdminInitiateAuthRequest()
      .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH)
      .withUserPoolId("***")
      .withClientId("***")
      .withAuthParameters(authParams)
    val authResult = mIdentityProvider.adminInitiateAuth(authRequest)
    authResult
}

上面的代码从aws cognito服务器返回accessToken,expiresIn,tokenType,refreshTokenidToken。根据AWS文档,当accessToken过期时,我们可以使用refreshToken获取新的accessToken或idToken以便继续用户会话。但是在文档中没有提到如何为此目的使用refreshToken。关于此的任何帮助将是可观的。预先感谢。

java scala amazon-cognito aws-java-sdk playframework-2.6
1个回答
0
投票

我自己弄清楚了。以下是工作代码

def refreshAccessToken(refreshToken: String): AuthenticationResultType = {
    val authParams: java.util.Map[String, String] = new java.util.HashMap[String, String]()
    authParams.put("REFRESH_TOKEN", refreshToken)
    val authRequest = new AdminInitiateAuthRequest()
      .withAuthFlow(AuthFlowType.REFRESH_TOKEN_AUTH)
      .withUserPoolId(***)
      .withClientId("***")
      .withAuthParameters(authParams)
    val authResult = mIdentityProvider.adminInitiateAuth(authRequest)
    val resultType: AuthenticationResultType = authResult.getAuthenticationResult
    resultType
  }
© www.soinside.com 2019 - 2024. All rights reserved.