Keycloak:REST API调用,用于通过管理员用户名和密码获取用户的访问令牌

时间:2020-07-01 18:46:15

标签: keycloak keycloak-rest-api

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

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

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

1 个答案:

答案 0 :(得分:-1)

有两种获取访问令牌的方法。一个带有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();