javascript - 使用 jwt 正确验证用户身份并防止攻击

标签 javascript node.js security authentication jwt

我正在阅读有关 JSON Web token 的好处的文章,但我发现了一些问题。

您可以将 token 设置为在一段时间后过期。但是,如果攻击者设法从客户端窃取 token 并使用它呢?是否可以检测到此事件并给出适当的响应。

此外,当 token 过期时,您必须制作另一个 token 并将其返还给客户端。我的问题是,与仅让客户端拥有不会过期的 token 相比,这样做的安全优势是什么。因为如果攻击者设法获得一次 token 。他们很有可能再次获得它

最后,如果每次都必须发出新的 token ,如何确保用户不必每次都重新登录,并且所有这些都可以在后台完成?

谢谢

最佳答案

You can set a token to expire after sometime. But if an attacker manages to steal the token from the client and use it? Is it possible to detect this event and give the appropriate response.

如果攻击者设法从客户端窃取 token 并使用它,则这是您无法控制的。这意味着客户的计算机已受到损害,他们有更大的事情需要担心(键盘记录器、屏幕阅读器等......)

Also, when a token expires, you have to make another token and give it back to the client. My question is what are the security benefits of this Vs just letting the client have a token that does not expire. Because if the attacker manages to.get the token once. There is a high chance they could get it again

重新颁发新的唯一 token 可确保攻击者必须始终了解最新的 token 。同样,如果他们能够再次捕获 token ,则在这种情况下您无能为力。

Lastly, if you have to issue a new token everytime, how can you make sure that the user does not have to re sign in every time and all of this can be done in the background?

您不应该信任过期的 token (假设黑客为您的网站找到了 3 天前的过期 jwt,它不应该允许他登录)。我建议您确定网站的敏感度,并确定它是否应该有 10 分钟、1 小时或 24 小时的有效期。我不会做超过24小时。

如果 token 已过期,您应该要求客户端重新登录。但是,如果它没有过期,也许您可​​以考虑发行新 token ,替换旧 token ,以便更新过期时间。

关于javascript - 使用 jwt 正确验证用户身份并防止攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595424/

相关文章:

javascript - 单击扩展程序图标时在新选项卡中打开扩展程序的 options.html 页面

javascript - jQuery 解析后获取未定义的 JSON

javascript - gremlin-javascript 中的纯文本 SASL 身份验证

java - DoS 攻击是开发者还是系统管理员的问题

PHP 安全扫描器

javascript - 如何在 Javascript 中一次更改对象的多个属性?

javascript - 裁剪元素内的文本

node.js - 如何避免嵌套 "node_module"目录?

node.js - 使用 Meteor.js 进行抓取

security - 根据页面实现 HTTP 或 HTTPS