提问人:Chris Barr 提问时间:11/7/2023 更新时间:11/7/2023 访问量:70
JWT 刷新令牌是否有替代品?
Are There Alternatives to JWT Refresh Tokens?
问:
我正在后端使用 NestJS (Node) 构建一个应用程序,在前端使用 Angular。我使用 JWT 进行身份验证。登录后,我发送访问令牌和到期持续时间(1 小时)。
{
"accessToken": "eyJhbGciOsdfvrNiwBInR5cCI6I4kpXVCJ9.xxxx.xxxx",
"expiresIn": 3600
}
在我的 Angular 应用程序中,每当我收到身份验证信息时,我都会将计时器重置为在持续时间后自动注销。所有这些信息都存储在 localStorage 中。所以问题是,登录后,即使您正在积极使用该应用程序,也会在 1 小时内自动注销。正如您所料,这很烦人。
private autoLogOut(expirationDurationMs: number): void {
this.autoLogoutTimer = setTimeout(() => {
this.logOut();
this.toastSvc.showWarning('You have been automatically logged out.', { autohide: false });
}, expirationDurationMs);
}
private getExpirationDate(expiresIn: number): Date {
const expiresInMs = expiresIn * 1000;
return new Date(new Date().getTime() + expiresInMs);
}
private handleAuthentication(data: IAuthResponse): void {
const expDate = this.getExpirationDate(data.expiresIn);
const authData = new AuthData(data.accessToken, expDate);
this.authDataChanged$.next(authData);
this.autoLogOut(data.expiresIn * 1000);
this.toastSvc.clear(); //clear any toasts that were hanging around (auto logout or bad password attempts)
localStorage.setItem(AccountService.STORAGE_KEY_TOKEN, data.accessToken);
localStorage.setItem(AccountService.STORAGE_KEY_EXPIRATION_DATE, expDate.toString());
this.accountChanged$.next(data.account);
}
我一直在阅读刷新令牌,到目前为止,我只对这个概念有所了解。我没有看到这与常规访问令牌之间有太大区别,只是它的存在时间更长。
JWT 刷新令牌是继续此处的最佳方式,还是我可以使用其他方法来重新颁发/更新 1 小时身份验证令牌?在当前令牌持续时间的~75%之后自动重新请求身份验证令牌是否是一种更简单的方法?示例:对于 1 小时令牌,如果您在 45 分钟后仍在使用该应用程序,它会自动将您再延长一小时。我喜欢这个想法的概念,但我几乎可以肯定,它周围有一些我没有看到的问题。
答: 暂无答案
评论