1 回答

TA貢獻1789條經(jīng)驗 獲得超8個贊
您可以使用授權客戶端 Java API。創(chuàng)建 AuthzClient 對象后,您可以將用戶名和密碼傳遞給AuthzClient#authorization(username, password)或AuthzClient#obtainAccessToken(username, password)方法以對用戶進行身份驗證并獲取訪問令牌(和/或 ID 令牌)在第一種情況下):
// create a new instance based on the configuration defined in keycloak-authz.json
AuthzClient authzClient = AuthzClient.create();
// send the authorization request to the server in order to
// obtain an access token granted to the user
AccessTokenResponse response = authzClient.obtainAccessToken("alice", "alice");
附帶說明一下,如果可能,您寧愿重用Keycloak Java 適配器之一來涵蓋更多功能,例如其他身份驗證方法(用戶通常被重定向到 Keycloack WUI,您可以在其中執(zhí)行非常靈活的身份驗證和授權策略)。
添加回答
舉報