為什麼每次發送請求時都刷新訪問令牌是個壞主意? (Why is it a bad idea to refresh access token every time when I sent request?)


問題描述

為什麼每次發送請求時都刷新訪問令牌是個壞主意? (Why is it a bad idea to refresh access token every time when I sent request?)

當我登錄我的應用程序時,我正在對本機應用程序進行反應並使用 OAuth2 並獲取訪問令牌、刷新令牌並及時過期。我在發送請求(GET、POST)時檢查了我的令牌是否及時過期。如果我的令牌過期,那麼我使用刷新令牌來獲取新的訪問令牌。我的同事告訴我,我不需要檢查過期時間,只需在每次發送請求時使用刷新令牌來獲取訪問令牌。我知道他的方式不對,但如果我用他的方式會發生什麼?為什麼每次發送請求都刷新訪問令牌不好?


參考解法

方法 1:

Because it increases the network round trips and makes your application slower than it needs to be, and increases the load on the token service.

That way lies scaling problems and terrible user experience.

方法 2:

Your co‑worker probably advised you to do this, which is how I always code these things:

  • Send the current access token to the API on each request
  • Eventually the access token will return 401
  • Then use the refresh token to get a new access token + retry the API call
  • Eventually the token renewal request will fail with an invalid_grant error and the user has to login again

That is, you refresh only when the access token expires and not on every single request. You avoid relying on the access token expiry time, since APIs can reject tokens for multiple reasons.

(by kiritotyJosh WulfGary Archer)

參考文件

  1. Why is it a bad idea to refresh access token every time when I sent request? (CC BY‑SA 2.5/3.0/4.0)

#oauth-2.0 #javascript #react-native #access-token #refresh-token






相關問題

OAuth2 用戶映射和丟失我的 Cookie (OAuth2 User Mapping and Loosing my Cookies)

如何在打開 Facebook 登錄對話框之前告訴 iPhone 應用用戶會發生什麼 (How to tell an iphone app user what will happen before opening Facebook login dialogue)

帶有 spring-security 的 OAuth2 - 通過 HTTP 方法限制 REST 訪問 (OAuth2 with spring-security - limit REST access by HTTP method)

帶有Phonegap 2.3.0的Facebook oAuth沒有在成功url返回令牌作為url參數 (Facebook oAuth with Phonegap 2.3.0 not returning token as url param at success url)

Dwolla API Key、Client_id 和 Client_Secret 有什麼區別? (What is the different between Dwolla API Key, Client_id & Client_Secret?)

Linkedin 應用程序具有 OAuth 用戶令牌和 OAuth 用戶密鑰,它們會過期嗎? (Linkedin Application has OAuth User Token and OAuth User Secret, Do they Expire?)

OAuth2:保護非用戶資源 (OAuth2: Protecting non-user resources)

IOS的foursquare oauth2 (foursquare oauth2 for IOS)

Spring Boot 2 Oauth 如何實現隱式代碼流 (Spring Boot 2 Oauth how to implement Implicit Code Flow)

FusionAuth - 基於邀請的用戶登錄與社交登錄 (FusionAuth - Invite based user on-boarding with social logins)

為什麼每次發送請求時都刷新訪問令牌是個壞主意? (Why is it a bad idea to refresh access token every time when I sent request?)

如何配置必須使用“密碼”授權類型從授權服務器請求令牌的客戶端 Java 應用程序? (How do I configure a client Java application which must request a token from an authorization server using a 'password' grant type?)







留言討論