简体   繁体   English

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

[英]Keycloak :REST API call to get access token of a user through admin username and password

I have a requirement where I need to get an access token of a user.我有一个要求,我需要获取用户的访问令牌。

I am aware of the admin username and password and hence can get the access token of the admin.我知道管理员用户名和密码,因此可以获得管理员的访问令牌。

Is there any rest API that can provide me access token a user using the above data?是否有任何 rest API 可以为使用上述数据的用户提供访问令牌?

There are two ways to get access token.有两种获取访问令牌的方法。 One with Rest client (keycloak Rest API) and other through java keycloak-admin-client library.一个带有 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();

I have the same requirement, however we dont want to pass this Clear Text Password in either of the above approach.我有同样的要求,但是我们不想在上述任何一种方法中传递这个明文密码。 We are thinking following approach but want suggesting if any best practices are already in place.我们正在考虑采用以下方法,但希望建议是否有任何最佳实践。

  • Step-1 : Create a secured end point at server end to return token from Keycloak.第 1 步:在服务器端创建一个安全端点以从 Keycloak 返回令牌。

  • Step-2 : While calling this end point first, Encrypt the password using some shared key at client end.步骤2 :首先调用此端点时,在客户端使用一些共享密钥加密密码。

  • Step-3 .步骤 3 At the receiving/server end decrypt the password with same shared key.在接收/服务器端使用相同的共享密钥解密密码。

  • Step-4 .步骤 4 Fetch the token from KeyCloak at server end and return it.在服务器端从 KeyCloak 获取令牌并返回。

If there are any other better approach, we should follow.如果有其他更好的方法,我们应该遵循。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM