Keycloak - Create Users/Roles in Realm (2024)
In previous tutorial we have installed the Keycloak. In this tutorial, we'll demonstrate how to create realm, client, users, roles and will map the user to the roles.
Keycloak Tutorial :
- Install Keycloak with MySQL
- Keycloak - Create Users/Roles in Realm
- Spring Boot Keycloak SSO Example
Q: What is Realm in Keycloak?
Realm is a central place to have users their credentials and rules.
Q: What is Client in Keycloak?
To connect to Keycloak from a Spring Boot application, each Spring Boot application must have a Client created in Keycloak. Client contains client ID and client secret to use with Spring Boot application.
Follow below steps to create Realm, Client, Users, Role and their mappings in the Keycloak.
Create Realm
- Access the keycloak from http://localhost:9090/auth and provide any username and password.
- Click on Administration Console.
- Provide same username and password as provided above.
- Once we've successfully logged in, we'll see the screen below.
- Roll over the mouse on Master dropdown to see Add Realm button. Click on the Add Realm.
- Provide the project name or organization name and click on create.
Take a look at our suggested posts:
Create Client
Each Spring Boot application will require client.
- Click on Clients -> Create.
- Create Client.
- Once click on Save, it'll navigate to Setting. Select Access Type -> Confidential.
- Download and start the project for Spring Boot Keycloak SSO Example. So that we can provide the the valid url of the project in Keycloak.
- Provide Valid Redirect URIs ->
http://localhost:8080/*
and click on Save. - Go to Credential tab. This credential will be needed in our spring boot application to connect with Keycloak.
Create Users
- Click on Users -> Add User. Provide username and click on save.
- Once user is created, click on Credentials tab.
- Provide Password.
- Turn ON the temporary Password and click on Set Password.
- It means that the first time a user signs in, it will prompt them to reset their temporary password and enter their own.
- If you turn it off, the user will be unable to change their password for the first time they sign in.
- We have created two users.
Create Roles
- Click on Roles -> Add role -> Provide Role Name.
- We have created two roles as ADMIN and USER.
Assign Roles to users
- Go to Users -> click on user -> Role Mappings -> Assign the specific role.
- You will see the roles has been assigned to the users.
- You can also see assigned roles from Roles -> Click on Role Name -> Users in Role tab.