我想知道在更改密码/注销时使 JWT 无效而不敲击 db 的最佳做法.
I would like to know the best practices to invalidate JWT without hitting db while changing password/logout.
我有以下想法通过访问用户数据库来处理上述两种情况.
I have the idea below to handle above 2 cases by hitting the user database.
1.如果密码更改,我检查存储在用户数据库中的密码(散列).
1.Incase of password changes, I check for password(hashed) stored in the user db.
2.在注销的情况下,我将上次注销时间保存在用户数据库中,因此通过比较令牌创建时间和注销时间,我可以使这种情况无效.
2.Incase of logout, I save last-logout time in user db, hence by comparing the token created time and logout time, I can able to invalidate this case.
但这两种情况的代价是每次用户点击 api 时都会点击用户 db.任何最佳做法都会受到赞赏.
But these 2 cases comes at the cost of hitting user db everytime when the user hits the api. Any best practise is appreciated.
更新:我认为我们不能在不点击 db 的情况下使 JWT 无效.所以我想出了一个解决方案.我已经发布了我的答案,如果您有任何疑问,欢迎您.
UPDATE: I dont think we can able to invalidate JWT without hitting db. So I came up with a solution. I have posted my answer, if you have any concern, you are welcome.
不使用刷新令牌时:
1.修改密码时:用户修改密码时,注意用户db中修改密码的时间,所以当修改密码时间大于token创建时间时,则token为无效.因此,剩余的会话将很快被注销.
1.While changing password: when the user changes his password, note the change password time in the user db, so when the change password time is greater than the token creation time, then token is not valid. Hence the remaining session will get logged out soon.
2.当用户注销时: 当用户注销时,将令牌保存在单独的数据库中(例如:InvalidTokenDB 并在令牌过期时从数据库中删除令牌).因此,用户从各自的设备上注销,他在其他设备上的会话不受干扰.
2.When User logs out: When the user logs out, save the token in a seperate DB (say: InvalidTokenDB and remove the token from Db when token expires). Hence user logs out from the respective device, his sessions in other device left undisturbed.
因此,在使 JWT 无效时,我遵循以下步骤:
Hence while invalidating a JWT, I follow the below steps:
关注上述方法:
使用刷新令牌时:访问令牌有效期为1天,刷新令牌为终身有效
When Refresh token is used: with expiry of access token as 1 day, refresh token as lifetime validity
1.修改密码时: 当用户修改密码时,修改用户的刷新令牌.因此,剩余的会话将很快被注销.
1. While changing password: When the user changes his password, change the refresh token of the user. Hence the remaining session will get logged out soon.
<强>2.当用户注销时:当用户注销时,将令牌保存在单独的数据库中(例如:InvalidTokenDB 并在令牌过期时从数据库中删除令牌).因此,用户从各自的设备上注销,他在其他设备上的会话不受干扰.
2. When User logs out: When the user logs out, save the token in a seperate DB (say: InvalidTokenDB and remove the token from Db when token expires). Hence user logs out from the respective device, his sessions in other device left undisturbed.
因此,在使 JWT 无效时,我遵循以下步骤:
Hence while invalidating a JWT, I follow the below steps:
关注上述方法:
注意:尽管 Hanz 在 Using Refesh Token in Token-based Authentication is secure? ,我听不懂他在说什么.任何帮助表示赞赏.
Note: Although Hanz suggested a way to secure refresh token in Using Refesh Token in Token-based Authentication is secured? , I couldn't able to understand what he is saying. Any help is appreciated.
所以如果有人有好的建议,欢迎您的意见.
So If anyone have nice suggestion, your comments are welcome.
更新:我正在添加答案,以防您的应用程序不需要具有生命周期到期的刷新令牌.此答案由 Sudhanshu (https://stackoverflow.com/users/4062630/sudhanshu-高尔).谢谢苏丹舒.所以我相信这是最好的方法,
UPDATE: I am adding the answer incase your app needs no refresh token with lifetime expiry. This answer was given by Sudhanshu (https://stackoverflow.com/users/4062630/sudhanshu-gaur). Thanks Sudhanshu. So I believe this is the best way to do this,
当不需要刷新令牌且访问令牌没有过期时:
当用户登录时,在他的用户数据库中创建一个没有过期时间的登录令牌.
when user login, create a login token in his user database with no expiry time.
因此,在使 JWT 无效时,请按照以下步骤操作,
Hence while invalidating a JWT, follow the below steps,
因此,使用这种方法,您不需要在数据库中存储注销令牌直到它们过期,也不需要在更改上述情况下需要的密码时存储令牌创建时间.但是,我相信这种方法仅在您的应用具有不需要刷新令牌且令牌没有到期的要求时才有效.
So with this approach, you don't need to store neither logout tokens in database until their expiry nor storing token creation time while changing password which was needed in the above cases. However I believe this approach only valids if your app has requirements with no refresh token needed and no expiry of the tokens.
如果有人对这种方法有疑虑,请告诉我.欢迎您的意见:)
If anyone has concern with this approach, please let me know. Your comments are welcome :)
这篇关于在 node.js 中更改密码和注销时使 JWT 无效的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!