【发布时间】:2016-05-20 20:15:16
【问题描述】:
我正在尝试找到一种正确的方法来刷新我从 Cognito 获得的 AWS.config.credentials。我正在使用开发人员授权的身份,它工作正常。我得到了凭证,如果我执行 refresh(),AWS.config.credentials.expireTime 也会按预期更新。
凭证在一小时后过期,所以我想我可以使用 setTimeout 刷新凭证并根据 credentials.expireTime 进行配置(我计算毫秒数)。
但是,我似乎必须更频繁地执行刷新。凭据在时间之前一直超时。如果我将延迟减少到更小的数量,setTimeout 方法就可以正常工作,但我不希望过度刷新。
这是真的吗?如果是这样,我需要多久执行一次?每 5 分钟左右刷新一次似乎有点过分了:/
定期刷新
function refreshAwsCredentials() {
clearTimeout(awsRenewalTimeout);
// perform credentials renewal
AwsService.refreshAwsCredentials()
.then( function () {
awsRenewalTimeout = setInterval(
function () {
refreshAwsCredentials();
}, AWS.config.credentials.expireTime.getTime() - new Date().getTime() - 300000
);
})
.catch( function (error) {
// checks error, normally it basically logs in, then refreshes
});
}
AwsService.refreshAwsCredentials()
if ( AWS.config.credentials.needsRefresh() ) {
AWS.config.credentials.refresh( function (error) {
if (error) {
// rejects promise with error message
}
else {
// resolves promise
}
});
}
【问题讨论】:
-
它们的有效期应为 1 小时。您如何检查您的凭据是否已超时?
-
我会定期检查 AWS.config.credentials.needsRefresh(),如果这表明我们需要更新,我会执行 AWS.config.credentials.refresh()。如果我然后检查 AWS.config.credentials.expireTime 我可以看到时间已延长
-
你能用一些代码更新这个问题吗?我发现this SO post 并不完全是重复的,但它讨论了您正在尝试做的基本设置。看看这是否有帮助。
标签: amazon-web-services timeout credentials amazon-cognito