我正在 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
声明的方式:
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. Thejti
claim can be used to prevent the JWT from being replayed. Thejti
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/