TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Question about best practices to revoking a JWT

1 点作者 mimg将近 9 年前
From what I have read I see two approaches. You can use a per-user secret to sign a token. When the user wants to invalidate, change the per-user secret. Of course this will invalidate all issued tokens so no fine grain control.<p>Another approach is to use the jti field. Store all the jti&#x27;s issued per user in a database. When a user wants to invalidate remove that jti. When receiving a token check if the jti is in the database for that user. Of course this is done after validating the integrity of the token and the database is kept up to date automatically removing jti’s that have expired. This offers fine grain control.<p>Do other approaches exist? Can either of these be considered best practice?

1 comment

ejcx将近 9 年前
I think that JWTs are not what you&#x27;re looking for.<p>JWT&#x27;s are meant to be stateless. Encrypt some claims, sign them, but no storage is necessary. A truly stateless system and the concept of revocation are mutually exclusive.<p>When you begin keeping track of nonces, jti&#x27;s, or whatever, you&#x27;ve lost this &quot;stateless&quot; property.<p>In my opinion you should be using short lived JWTs and then don&#x27;t worry about revocation.<p>About your specific questions. There are many pitfalls and operational issues associated with your solution.<p>- Changing a per user key is complicated. If it&#x27;s a shared secret you have to securely redistribute it. If it&#x27;s asymmetric key you need to republish the public keys, and have consumers poll to get the new key. Both of these kind of suck.<p>- The JTI thing is fine if you follow the spec and have a strongly random JTI.<p>Either way, you&#x27;re trying to solve a problem that JWT is not meant to solve. Signing a new JWT and regenerating new JWTs is a cheap operation. Keep them short-lived, keep it simple.