我应该在哪里保存JWT刷新令牌?

发布于 2025-01-25 22:16:54 字数 512 浏览 1 评论 0原文

我使用了Nodejs Express和EJS和PassProt JWT。

我通过httponly属性保存了JWT令牌。

在渲染页面之前,router.get('/',isauth.verifyToken(),admincontroller.checkuser); ,检查令牌是否有效。

如果令牌无效,请将其重定向到登录页面。

exports.verifyToken = ()=>{
    return passport.authenticate('cookie', { session: false, failureRedirect: '/users/login' });   
}

现在,我不仅要使用访问令牌,还要使用刷新令牌。

我应该在哪里保存刷新令牌?

我认为,在cookie中保存访问令牌和刷新令牌不是答案。

将刷新令牌存储在本地存储中是正确的吗?

如果本地存储是正确的,那么刷新令牌的逻辑应该在哪里?

I used Nodejs Express and ejs and passprot jwt.

I saved jwt token in the cookie by httpOnly the attribute.

And before the page is rendered,router.get('/',isauth.verifyToken(), adminController.checkUser);
,check if the token is valid.

If the token is not valid, redirect it to the login page.

exports.verifyToken = ()=>{
    return passport.authenticate('cookie', { session: false, failureRedirect: '/users/login' });   
}

Now, I want to use not only the access token but also the refresh token.

where should I save the refresh token?.

In my opinion, saving both access token and refresh token in cookies is not the answer.

Is it right to store refresh token in local storage?.

If local storage is correct, where should the logic of refreshing token?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2025-02-01 22:16:54

如果您的后端可以进行身份​​验证,发出令牌然后消耗它们,那么就无需发出单独的刷新令牌。您可以依靠访问令牌。实际上,在这样的设置中,您正在使用HTTP会话,因此您甚至不需要JWT。如果您有一个单独的授权服务来发出代币,那么最好将刷新令牌存储在您的后端中 - 在服务中,最终将拨打授权服务以获取新令牌。

无论如何,不​​要将刷新令牌存储在本地存储中。将令牌保留在那里是不安全的,因为它们容易受到XSS攻击的影响。

If you have one backend that authenticates, issues tokens, and then consumes them, then there's no need to issue a separate refresh token. You can just rely on the access token. In fact, in such a setup, you're using an HTTP session, so you don't even need a JWT. If you have a separate authorization service that issues tokens, then it's best to store refresh tokens in your backend - in the service that will eventually call the authorization service to get new tokens.

In any way, don't store refresh tokens in the local storage. It's not safe to keep tokens there as they are vulnerable to XSS attacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文