node.js - jti claim 应多久重新生成一次?

标签 node.js express jwt

我正在 REST API 中实现基于 JWT 的身份验证系统,并希望在 token 中使用 JWT_ID 声明。根据Auth0 ,JWT ID 允许使用一次 token :

jti (JWT ID): Unique identifier; can be used to prevent the JWT from being replayed (allows a token to be used only once)

我想知道 JWT ID 应该多久重新生成一次?

  • 满足每个请求
  • 仅登录时
  • token 刷新时(如果使用刷新 token 系统)

注意:我没有使用 Auth0 进行身份验证。

最佳答案

请记住,jti 声明的使用是可选,因此您根本不需要使用它。但是,当您需要跟踪白名单或黑名单中的 token 时,它非常有用。

I am wondering how often should the JWT ID be regenerated?

我不确定你所说的“重新生成”是什么意思。

应在生成 token 时分配 jti 声明的值。一旦 token 为 signed ,没有办法在不使签名无效的情况下修改它。因此,如果您在每个请求中为 token 分配一个新的 ID,则必须再次对该 token 进行签名,这样您最终会为每个请求获得一个新 token 。

以下是 RFC 7519 中定义 jti 声明的方式:

4.1.7. jti (JWT ID) Claim

The jti (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that here is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The jti claim can be used to prevent the JWT from being replayed. The jti value is a case-sensitive string. Use of this claim is OPTIONAL.


底线

根据我对您的用例的理解,当客户端进行身份验证以及客户端刷新 token 时,颁发新 token (具有唯一的 ID,例如 UUID )是有意义的。

值得一提的是,jti 声明本身对于 token 重放没有任何作用。但是,它为您提供了识别 token 并在白名单或黑名单中跟踪它们的方法。

关于node.js - jti claim 应多久重新生成一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56059302/

相关文章:

javascript - 如何使用 Express 和 AngularJS 打开带有请求的新链接

python - 有时会出现乱码结果,通过 TCP 套接字将 base64 编码图像从 python 发送到 node.js

javascript - 将 Create-React-App 与 ExpressJs 结合使用

node.js - 如何编写 Mocha 测试,其中一个测试取决于下一个测试的结果?

security - 将 jwt 作为 GET 请求的查询参数放入 url 是否安全?

angularjs - 无法理解 JWT 和 Java (Spring Boot)。这个应用程序安全吗?

javascript - 刷新 JWT token - 这样做吗?什么时候?

node.js - 发送电子邮件 sendgrid 在促销选项卡中收到,而不是在主要选项卡中

node.js 电子邮件验证 token

node.js - app.post 不工作node.js