Skip to content
Advertisement

How to get user details from keycloak (not logged in user but any user by his id)

In my Java web application with Spring Boot I have Keycloak authentication done but now I need to get user details of some user by id (not user that is logged in). Is it possible ? I want to get his roles, groups and data from Java code.

Advertisement

Answer

You can use the REST API by getting an access token from:

@POST ${yourHost}/auth/realms/${yourRealm}/protocol/openid-connect/token

with @RequestBody

{
  "client_id": "admin-cli",
  "username": "admin",
  "password": "secret",
  "grant_type": "password"
}

Then, call ${_HOST}/auth/admin/realms/${_REALM}/users

using that token as a Header’s parameter "Authorization: bearer " + ${_TOKEN}

Do the same to get userByID, but by calling: ${_HOST}/auth/admin/realms/${_REALM}/users/${_USER_ID}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement