In this article, we’ll be covering the following topics :
Installing and setting up a keycloak server in your machine
Keycloak integration with a Spring Boot application
Using Spring Security
What is Keycloak?
Keycloak is an open source identity and access management solution which mainly aims at applications and services.
Users can authenticate with Keycloak rather than individual applications.
So, the applications don’t have to deal with login forms, authenticating users and storing users. Once logged-in to Keycloak, users don’t have to login again to access different applications.
Same thing is applicable to sign-out. Keycloak offers everything a sophisticated user management tool needs – without having to log on repeatedly with every login and into every system-as well as system security, social logins, support for mobile apps and integration into other solutions.
In simple terms, Keycloak serves as a solution to manage authentication and authorization features. For more details, you can check out the official documentation available in the official website.
Installing and setting up Keycloak
Keycloak can be used as a standalone application or with Docker/Kubernetes. For this article we will use the Standalone server distribution.
Download the Standalone server distribution here. The latest version as of now is 11.0.0.
If you’re using linux, make sure to download the tar.gz version.
Once downloaded, extract the folder and start Keycloak from the terminal.
Now with Keycloak up and running, open up a browser and visit http://localhost:8180 to create an admin login.
After setting up an username (e.g. codefiction) and a password, click Create and you should expect a “User created” message to assert everything worked.
Go to the Administrative Console and enter the admin credentials you just registered.
Getting started with Keycloak
After logging in, a default Master realm should show up.
Navigate to the upper left corner to discover the “Add realm” button and add a new realm called Spring-Boot-Keycloak.
After creating a new realm, we should be redirected to our Spring-Boot-Keycloak realm configuration where all the following operations will be executed.
Creating a new client
Keycloak comes with built-in clients that you can check by navigating to the Clients page.
To create a client for our application, we can click the “Create” button on the upper right corner of the clients table.
Set a client id for identification and click “Save”.
In the next screen, we can leave all the default configuration intact except for the “Valid Redirect URIs field” where we need to specify the application URL that will be used in this client for authentication.
Setting up a Role and a User
Since Keycloak uses Role-Based access, all users must have a defined role.
Navigate to the “Roles” page and add a role by clicking the “Add Role” button in the upper right corner of the roles table.
To create a user role, specify a name and a description.
Now that we have a user role, we may go to the “Users” page and add some data to it.
Click the “Add user” button, specify the username, first name and last name of an user and click “Save” to display a page with the details envolved.
To set a password to the defined used, go to the “Credentials” tab next to “Attributes” and “Role Mappings”.
Now to assign the user role to our user_one, navigate to the “Role Mappings” tab, select the user role in the “Available Roles” section and move it to the “Assigned Roles”.
How to generate an Access Token with Keycloak’s API
To create our login page, we’ll use Keycloak’s REST API to generate and refresh access tokens through the following steps :
Acquire an access token by sending a POST request to :
Since the Keycloak Spring Boot adapter is fully integrated with Spring Boot’s auto-configuration, all we need to do is add the Keybloak Spring Boot starter to our project.
Make sure to include in your maven pom.xml file :
In the application.properties file, we enter the basic configuration for our Keycloak integration :
// Set here the path specified in keycloak.auth-server-url
// Set here your realm name
// Set here the client named in the admin console
As security constraints (not needed because we’re using Keycloak Spring Security Adapter) :
These ensure that all requests sent to /users/* will only be authorized if it’s requested by someone with the user role.
As additional configuration, we set the following to populate our controller’s Principal with a proper user :
Running the application
To test our application, start it through the IDE and visit http://localhost:8081.
Once we log in as the user we defined previously, Keycloak verifies our authorization and redirects us to the users files page with our example data.
In the next posts we will cover using oauth2, registering and logging in using Google, Github, etc.
All source code from this article is available here in our GitHub page, feel free leave a comment or suggestion below for what you’d like for us to cover in the next article.