4

我有一个要求,我需要获取用户的访问令牌。

我知道管理员用户名和密码,因此可以获得管理员的访问令牌。

是否有任何其他 API 可以为使用上述数据的用户提供访问令牌?

4

1 回答 1

4

有两种获取访问令牌的方法。一个带有 Rest 客户端(keycloak Rest API),另一个通过 java keycloak-admin-client 库。

1. Keycloak Rest API:

URI: http://keycloak:8080/auth/realms/myrealm/protocol/openid-connect/token
Type: POST
Content-Type: application/x-www-form-urlencoded
grant_type:password
username:user
password:user_password
client_id:client_id
secret_id:client_secret

2. Keycloak admin client (JAVA)

Keycloak instance = Keycloak.getInstance("http://keycloak:8080/auth", "myrealm", "user", "user_password","client_id", "client_secret");                                                                                                      
TokenManager tokenmanager = instance.tokenManager();
String accessToken = tokenmanager.getAccessTokenString();
于 2020-07-03T11:48:31.940 回答