在更改密码和在node.js中注销时使JWT无效的最佳做法? [英] Best practices to invalidate JWT while changing passwords and logout in node.js?

查看:218
本文介绍了在更改密码和在node.js中注销时使JWT无效的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在更改密码/注销时不使用db而使JWT无效的最佳做法。

I would like to know the best practices to invalidate JWT without hitting db while changing password/logout.

我有以下想法通过点击用户数据库处理上述2个案例。

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时命中用户数据库为代价的。任何最佳实践都表示赞赏。

But these 2 cases comes at the cost of hitting user db everytime when the user hits the api. Any best practise is appreciated.

更新:
我认为我们无法在没有命中数据库的情况下使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.

推荐答案

当使用No Refresh令牌时:

1.更改密码时:当用户更改密码时,请注意用户数据库中的更改密码时间,因此,当更改密码时间大于令牌创建时间时,令牌无效。因此剩下的会话很快就会被注销。

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.当用户注销时:当用户注销时,将令牌保存在单独的DB(例如:InvalidTokenDB并在令牌过期时从Db中删除令牌)。因此,用户从相应设备注销,他在其他设备中的会话保持不受干扰。

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. 检查令牌是否有效。

  2. 如果有效,请检查它是否存在于invalidTokenDB(记录的数据库中) out标记存储到其到期时间。)

  3. 如果它不存在,则检查令牌创建时间并更改用户db中的密码时间。

  4. 如果更改密码时间<令牌创建时间,然后令牌有效。

  1. Check whether the token is valid or not.
  2. If valid, check it is present in invalidTokenDB (a database where logged out tokens are stored till their expiry time).
  3. If its not present, then check the token created time and changed password time in user db.
  4. If changed password time < token created time, then token is valid.

关注上述方法


  1. 对于每个api请求,我需要按照上述所有步骤进行操作,这可能会影响性能。

使用刷新令牌时,访问令牌到期为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并在令牌过期时从Db中删除令牌)。因此,用户从相应设备注销,他在其他设备中的会话保持不受干扰。

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. 检查令牌是否有效

  2. 如果有效,请检查令牌是否存在于InvalidTokenDB中。

  3. 如果不存在,请使用userDB中的刷新令牌检查刷新令牌。

  4. 如果等于,则为有效令牌

  1. check whether the token is valid or not
  2. If valid, check whether the token is present in InvalidTokenDB.
  3. If not present, check the refresh token with the refresh token in userDB.
  4. If equals, then its a valid token

关注上述方法


  1. 对于每个api请求,我需要按照上述所有步骤进行操作,这可能会影响性能。

  2. 如何使刷新令牌无效,因为刷新令牌没有效力,如果它被黑客使用,仍然认证是有效的,请求将始终成功。

注意:虽然Hanz建议在在基于令牌的身份验证中使用Refesh令牌是安全的吗?,我无法理解他在说什么。任何帮助表示赞赏。

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--给出野牛的)。谢谢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,


  1. 检索用户信息并检查令牌是否在他的用户数据库中。如果允许的话。

  2. 当用户注销时,只从他的用户数据库中删除此令牌。

  3. 当用户更改其密码时,删除所有令牌他的用户数据库并要求他再次登录。

因此,使用这种方法,您无需在数据库中存储任何注销令牌直到它们到期,并且在更改上述情况下所需的密码时存储令牌创建时间。但是我相信这种方法只有在你的应用程序没有需要刷新令牌并且没有令牌到期的要求时才有效。

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无效的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆