久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 node.js 中更改密碼和注銷時使 JWT 無效的最佳實

Best practices to invalidate JWT while changing passwords and logout in node.js?(在 node.js 中更改密碼和注銷時使 JWT 無效的最佳實踐?)
本文介紹了在 node.js 中更改密碼和注銷時使 JWT 無效的最佳實踐?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想知道在更改密碼/注銷時使 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. 檢查令牌是否有效.
  2. 如果有效,請檢查它是否存在于 invalidTokenDB(已注銷的令牌存儲到其到期時間的數據庫)中.
  3. 如果不存在,則檢查用戶 db 中的令牌創建時間和更改密碼時間.
  4. 如果更改密碼時間
  5. 令牌創建時間,則令牌有效.

關注上述方法:

  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 并在令牌過期時從數據庫中刪除令牌).因此,用戶從各自的設備上注銷,他在其他設備上的會話不受干擾.

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. 對于每個 api 請求,我都需要按照上述所有步驟操作,這可能會影響性能.
  2. 如何使刷新令牌無效,因為刷新令牌沒有有效性,如果被黑客使用,仍然認證有效,請求將始終成功.

注意:盡管 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,

  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 無效的最佳實踐?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: 国产高清在线视频 | 欧美日韩国产一区二区 | 日韩一区二区三区在线观看 | 91大片 | 成人欧美一区二区三区黑人孕妇 | 欧美乱淫视频 | 99视频在线免费观看 | 久www | 国产精品久久久久久久久免费高清 | av二区三区 | 日本亚洲欧美 | 色就干| 国产成人免费 | 青青草视频网 | 91精品久久久久久综合五月天 | 亚洲高清在线观看 | 欧美一级片在线观看 | 中文字幕亚洲精品在线观看 | 国产成人精品一区二区三区在线观看 | 久久久久国产精品一区二区 | 亚洲免费成人av | 国产1区在线 | 99精品国产一区二区青青牛奶 | 国产激情网站 | 亚洲成人一区二区三区 | 精品一区二区三区在线观看 | 欧美日韩午夜精品 | 福利社午夜影院 | 激情在线视频网站 | 色影视 | 国产精品国产三级国产aⅴ原创 | 人人色视频 | 欧美精品一区二区三区蜜桃视频 | 久久日韩精品一区二区三区 | 日韩欧美二区 | 中文字幕 在线观看 | 伦理午夜电影免费观看 | 欧美精品中文字幕久久二区 | 国产精品成人免费 | 国产精品电影网 | 日韩中文在线视频 |