android - 如文档中所述,如何使访问或 id 令牌的寿命延长 10 年?
问题描述
我正在使用 Amazon Cognito 进行身份验证,我想问一下我的访问令牌和 id 令牌是否会在一小时内过期,此外,我已经经历了许多他们告诉使用刷新令牌的答案,因为它们的有效期为 10 年,所以我的问题是如何在 android 中使用刷新令牌?
现在登录后,我正在将CognitoCachingCredentialProvider
登录映射设置为令牌 - 我同时使用 Facebook 和电子邮件登录。
val authenticationHandler = object : AuthenticationHandler {
override fun onSuccess(userSession: CognitoUserSession?, newDevice: CognitoDevice?) {
//After Authentication User Cognito Access Id and Access Secret Extraction
currentSession = userSession!!
//Getting Session Token
val id = currentSession.idToken.jwtToken
//Credential Provider
val cognitoCachingCredentialsProvider = CognitoCachingCredentialsProvider(this@LoginActivity,resources.getString(R.string.cognito_identity_pool_id),Regions.myRegion)
cognitoCachingCredentialsProvider.clear()
//Login Map
val login = HashMap<String,String>()
login["myString"] = id
cognitoCachingCredentialsProvider.logins = login
//Off the main thread
SimpleAsyncTask(this@LoginActivity,cognitoCachingCredentialsProvider).execute()
}
override fun authenticationChallenge(continuation: ChallengeContinuation?) {
continuation?.continueTask()
}
override fun getAuthenticationDetails(authenticationContinuation: AuthenticationContinuation, userId: String) {
// The API needs user sign-in credentials to continue
Log.d(TAG, "userId is : $userId")
val authenticationDetails = AuthenticationDetails(userId, password, null)
authenticationDetails.authenticationType = "USER_PASSWORD"
// Pass the user sign-in credentials to the continuation
authenticationContinuation.setAuthenticationDetails(authenticationDetails)
// Allow the sign-in to continue
authenticationContinuation.continueTask()
}
override fun getMFACode(multiFactorAuthenticationContinuation: MultiFactorAuthenticationContinuation) {
// Multi-factor authentication is required; get the verification code from user
multiFactorAuthenticationContinuation.setMfaCode(null)
// Allow the sign-in process to continue
multiFactorAuthenticationContinuation.continueTask()
}
override fun onFailure(exception: Exception) {
// Sign-in failed, check exception for the cause
Log.e(TAG, "${exception.message}")
}
}
// Sign in the user
user.getSessionInBackground(authenticationHandler)
}
internal class SimpleAsyncTask(private val activity: Activity,private val credential:CognitoCachingCredentialsProvider) :
AsyncTask<Void, Void, Void>() {
override fun doInBackground(vararg p0: Void?):Void ?{
credential.refresh()
credential.setPersistenceEnabled(true)
return null
}
类似的代码也用于像这样的facebook登录
FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
//Getting access Token
val accessToken = loginResult.accessToken.token
//Credentials Extraction
val credentials = CognitoCachingCredentialsProvider(this@LoginActivity,resources.getString(R.string.cognito_identity_pool_id),Regions.myRegion)
credentials.clear()
//Map of login
val login = HashMap<String,String>()
login["graph.facebook.com"] = accessToken
//Setting the value of map
credentials.logins = login
//Off the main thread
SimpleAsyncTask(this@LoginActivity,credentials).execute()
}
override fun onCancel() {
//Cancel code
Toast.makeText(this@LoginActivity,"Canceled",Toast.LENGTH_SHORT).show()
}
override fun onError(exception: FacebookException) {
//Error code
Toast.makeText(this@LoginActivity,exception.toString(),Toast.LENGTH_SHORT).show()
}
})
现在我用它来检查用户登录状态,我检查cognitoCachingCredentialProvider.cachedId!=null
检查用户登录的条件。但是它登录了一个小时如何让用户长时间登录
解决方案
刷新令牌与 id 或访问令牌明显不同。您可以使用刷新令牌来获取新的访问权限和 id 令牌(顾名思义)。当您调用 getSession 时,如果您的令牌已过期并且您的刷新令牌尚未过期,它应该会自动刷新您的令牌。
推荐阅读
- python - 如何将模型应用于以前看不见的验证集:拟合错误期间的不同形状
- python - 尝试在 python 中导入 tensorflow 库时收到错误(Anaconda3 上的 Jupyter)
- javascript - 用地图替换嵌套对象
- c# - 如何在 C# 中编写动态 lambda 表达式的一部分,其中包含其中的操作
- android - 谷歌移动广告实施减慢了 Flutter App
- typescript - tslint 在我的 TypeScript 项目中不起作用
- python - 有什么方法可以生成数字,同时始终包含特定数字而不创建任何重复项?- Python
- python - 在python中,为什么hash()函数在给定int时返回确切的输入值
- c# - 如何删除文本框1中字符串中剩余的最后一个单词
- javascript - SERVER 显示 METRO 徽标和捆绑程序而不是 react-native