我的 ID 令牌有效期为一小时。一小时后,当我在服务调用中对 CognitoUser 对象调用 getSessionInBackground 时,我的调用失败。如果前一个 ID 令牌已过期,这不应该返回一个新的 ID 令牌吗?这种行为不能始终如一地重现并无济于事 - 它只发生在某些屏幕上。此外,它似乎只发生在 Android 中,而不是 iOS。
问问题
522 次
1 回答
1
发生这种情况是因为 sdk 中的线程不是完全安全的,这个错误发生在 aws-sdk-version <2.3 中,因为一个线程读取了正确的值,如果令牌过期,它会清除缓存的令牌,这样第二个线程就什么也没有了读取,第二个线程假定不存在令牌(当第一个线程刷新令牌时),因此抛出用户未授权异常。
如果您使用的是 aws-sdk2.2,这是一个突出的错误,您可以使用以下代码片段来修复它:
new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
CognitoUser user = userPool.getCurrentUser();
user.getSessionInBackground(new AuthenticationHandler() {
@Override
public void onSuccess(final CognitoUserSession userSession) {
Log.d("Authenticator.java", userSession.getIdToken().getJWTToken().toString());
semaphore.release();
callback.onSuccess(userSession);
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String UserId) {
semaphore.release();
callback.onNeedsPassword();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
semaphore.release();
callback.onNeedsPassword();
}
@Override
public void onFailure(final Exception exception) {
semaphore.release();
callback.onFailure(exception);
}
});
}
}).start();
当前的 aws-sdk-2.4 版本中还有一个错误,与此相关,如果您多次调用 aws 以获取新的 ID 令牌,则会增加您的网络流量。(这是一个边缘情况,但仍然) https:// github.com/aws/aws-sdk-android/pull/272
于 2017-03-29T23:12:17.330 回答