久久久久久久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在线观看免费视频 | 久在线观看 | 久久久久久网 | 日韩欧美视频在线 | 午夜免费视频 | 久久久网| 久久久久国产一区二区三区四区 | 麻豆精品国产免费 | 一区二区三区在线电影 | 午夜久久久 | 午夜爽爽爽男女免费观看 | 亚洲精品日韩在线 | 国精产品一区一区三区免费完 | 99pao成人国产永久免费视频 | 精品一区二区电影 | 日韩精品视频在线播放 | 国产精品国产精品国产专区不片 | 欧美日韩网站 | 精品九九 | 欧美三级久久久 | 国产成人在线视频播放 | 在线免费观看黄a | 国产精品免费小视频 | 视频在线亚洲 | 99福利网| 日韩在线不卡 | 日韩三片| 夜夜骚视频 | 伊人久操| 日韩www | 你懂的在线视频播放 | 久久一| 在线视频一区二区 | 久久综合一区 | 国产精品一区二区三区四区五区 |