Keycloak Installation using Docker
Keycloak is an open source identity and access management solution.
This guide aims to walk you through the setup of Keycloak using Docker, facilitating easy authentication and authorization management within the application.
To set up Keycloak as a Docker image, follow these steps:
This command installs and starts Keycloak at the specified endpoints, locally accessible at http://localhost:8080.
Once logged in:
This section guides you through the initial setup and configuration of Keycloak through its administration console.
- Go to administration console
- Login using username: admin, password: admin.
- Go to master and add a new realm as “credebl-platform”.
- Go to the client and add a new client as a clientId = “adminClient”.
- Click on Next Button
- Set client authentication to ON mode.
- Set Service Accounts Roles to ON mode.
- Set Direct Access Grants to ON mode.
- Click on Next Button
- Add http://your-url/* (Eg.: http://localhost:3000/*) in Valid Redirect URIs as front-end url.
- Add http://your-url/ (Eg.: http://localhost:8080/) in Root URL.
- Add http://your-url/ (Eg.: http://localhost:8080/) in Web Origins.
- Save the details
- Goto Realm settings and click on the Tokens tab section
- Set Revoke refresh token ON.
- Set Access tokens lifespan.
- Click on the Save button.
Service Account Roles Settings:
This section outlines the configuration of service account roles to manage permissions and access control within Keycloak.
- Go to adminClient by selecting from the clients side tab.
- Go to the Service account role section.
- Click on the Assign role button.
- Select Filter by clients from the dropdown menu.
- Select the below roles from the Available Roles and click on the Assign button:
- Create-client
- Manage-client
- Manage-users
- Query-clients
- Query-users
- View-clients
- View-users
Realm Roles Settings:
This section covers the configuration of realm role settings within Keycloak.
- Under the credebl-platform realm, from the menu select realm-roles.
- In realm-roles, click on Create Role and create a role with Role name=“holder”, as when a new user registers on the platform we are assigning him a “holder” role from the realm.
SSO Session Settings:
Set SSO Session Idle to 2 days. This is the expiration time of the refresh token if the user is idle on the platform. We can configure this as per our need.
Update the .env
This section covers the setup of environment variables for seamless integration with your application
Update the .env file for the Keycloak details:
To set the env variable KEYCLOAK_MANAGEMENT_CLIENT_SECRET, in credebl-platform realm, go to clients >> adminClient. Now in the Credentials tab, copy the client secret
Add users manually (Optional):
This section provides guidance on adding users manually to the Keycloak realm.
- Go to users.
- Add username: your.email@your.domain and email: your.email@your.domain.
- Enable user and email, then click on save.
- Change the password in the credential section for user login.
- Update the Keycloak userId in the users table in the local database.
- Also update the client secret key in the .env file, which you will get in the credential section of adminClient in the client section.
- Test the flow by logging in to platform admin.