文章出處

如果使用 IdentityServer4 做授權服務的負載均衡,默認情況下是不可以的,比如有兩個授權服務站點,一個資源服務綁定其中一個授權服務(Authority配置),如果通過另外一個授權服務獲取access_token,然后拿這個access_token去訪問資源服務,會報 401 未授權錯誤,為什么?原因在這:

By default an access token will contain claims about the scope, lifetime (nbf and exp), the client ID (client_id) and the issuer name (iss).

歸納一下,生成access_token受影響的因素:

  • scope(授權范圍):服務包含在 scope 內,生成的access_token,才能訪問本服務。
  • lifetime(生命周期):過期的access_token,無效訪問。
  • client ID (client_id):不同的客戶端 ID,生成不同對應的access_token
  • issuer name (iss):翻譯過來“發行者名稱”,類似于主機名。
  • RSA 加密證書(補充):不同的加密證書,生成不同對應的access_token

要讓負載均衡下的兩個授權服務,可以正常使用的話,需要確保兩臺授權服務以上五種因素完全一致,除了 issuer name (iss),其他因素都是一樣的。

IdentityServer4 怎么設置 issuer name (iss)呢?答案是通過IssuerUri

  • IssuerUri:Set the issuer name that will appear in the discovery document and the issued JWT tokens. It is recommended to not set this property, which infers the issuer name from the host name that is used by the clients, If not set, the issuer name is inferred from the request.

說明中不建議我們進行設置,默認情況下,IdentityServer4 會從客戶端的主機名中獲取,可以認為,默認情況下,issuer name(IssuerUri)就是授權服務的主機名(比如http://10.9.1.1:5000)。

手動設置IssuerUri代碼:

var builder = services.AddIdentityServer(x => x.IssuerUri = "http://111.12.2.21:8000"); //slb 地址

資源服務授權配置修改:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://111.12.2.21:8000", //slb 地址
    ApiName = "trade_refund",
    RequireHttpsMetadata = false
});

獲取access_token示例代碼:

var client = new DiscoveryClient("http://111.12.2.21:8000"); //必須是 slb 地址,如果是單獨的授權服務地址,會報錯誤(Value cannot be null. Parameter name: address)
client.Policy.RequireHttps = false;
var disco = await client.GetAsync();
var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = tokenClient.RequestClientCredentialsAsync(scope);
var accessToken = tokenResponse.AccessToken; 

通過 HTTP Post 獲取access_token(不通過 slb,直接請求單獨的授權服務),可以授權訪問資源服務,獲取access_token示例:

參考資料:


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()