gpt4 book ai didi

java - Cognito用户池: How to use refreshToken to get new accessToken after accessToken gets expired in aws cognito java sdk?

转载 作者:行者123 更新时间:2023-12-01 18:36:15 25 4
gpt4 key购买 nike

我在基于 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,以便继续用户 session 。但在文档中没有提到如何使用refreshToken来达到此目的。任何有关这方面的帮助将是值得赞赏的。提前致谢。

最佳答案

我自己想出来了。以下是工作代码

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
}

关于java - Cognito用户池: How to use refreshToken to get new accessToken after accessToken gets expired in aws cognito java sdk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60035651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com