The provided content is a comprehensive guide on integrating Keycloak as an Identity Provider (IdP) with a Spring Boot application called StarVote, detailing the setup, configuration, and testing processes.
Abstract
The article offers a step-by-step tutorial on enabling Keycloak as an Identity Provider for the StarVote application, a single Spring Boot application. It covers the initial setup of Keycloak using Docker, the configuration of realms, clients, roles, and groups within Keycloak, and the necessary modifications to both the frontend and backend of the StarVote application to support authentication through Keycloak. The guide also includes instructions for testing the application's user interface and API endpoints with Keycloak authentication, ensuring that only authorized users can access the application. Additionally, the article provides insights into inspecting JWT tokens and preparing for the next steps with Okta as an alternative IdP.
Opinions
The author emphasizes the importance of not disabling the "Verify Profile" required action in a production environment, highlighting a best practice for security.
The creation of roles and groups within Keycloak is presented as a flexible way to manage user permissions, suggesting a robust approach to role-based access control.
By providing a Bearer access token for API requests, the article demonstrates a secure method for accessing protected resources in the application.
The use of Docker for running Keycloak is assumed to be a straightforward and convenient approach for readers familiar with containerization.
The article encourages reader engagement and support for the author's work, indicating a desire for community interaction and feedback.
The author's enthusiasm tone when setting a password for the admin user (using "123" 😝) adds a touch of humor to the technical content, making it more relatable and engaging.
The mention of alternative IdPs, such as Okta, suggests an open and versatile approach to identity management, catering to different preferences and requirements in the industry.
Spring Boot | Star Vote
Building a Single Spring Boot App with Keycloak or Okta as IdP: Enabling Keycloak as IdP
A step-by-step guide on how to enable Keycloak as Identity Provider to the StarVote application
This article is part of a series that explores the implementation of a Single Spring Boot application called StarVote. The application will use Keycloak or Okta as Identity Provider.
In the introductory article, we outline the sections we will cover:
Here’s a sneak peek of how the StarVote application will be at the end!
In this particular article, we will explore how to enable authentication using Keycloak as an Identity Provider, ensuring only authorized users can access the application.
So, let’s get started!
Starting Keycloak
Note: Before proceeding, please ensure that you have Docker installed.
To get started, open a terminal and execute the following command to launch the Keycloak Docker container.
On the Sign in screen, type admin for both “Username” and “Password” fields;
Click the Sign in button.
Create a new Realm
On the left menu, click the dropdown button that contains Master.Then, click Create Realm ;
Set my-realm to “Realm name” field;
Click Create button.
Disable the Required Action Verify Profile
Note: Just for this demo. Do not do it in production!
On the left menu, select “Authentication”;
In the “Authentication” screen, click “Required actions” tab;
Disable the “Verify Profile” required action.
Create a new Client
On the left-hand menu, select “Clients”;
In the “Clients” screen, click Create client;
In the “General Settings” tab, set star-vote as the “Client ID” and click Next;
In the “Capability config” tab, enable the “Client authentication” toggle switch and click Next;
In the “Login Settings” tab, set http://localhost:8080 in the “Root URL” field and click Save.
The Client secret generated for the star-vote client can be found in the “Credentials” tab. Make a note of this value, as it will be needed later.
Finally, let’s create two new roles for the star-vote client users.
Navigate to the “Roles” tab and click Create Role;
EnterSTAR-VOTE-ADMIN as the “Role Name” and click Save;
Repeat the process to create the STAR-VOTE-USER role.
Create new Groups
In the left-hand menu, select “Groups”;
In the “Groups” screen, click Create group;
Enter StarVoteAdmins for the “Name” and click Create;
Click on the name of the StarVoteAdmins group to select it;
Select the “Role mapping” tab and click Assign role;
Choose “Filter by clients” and type STAR in the “Search by role name” field. The STAR-VOTE-ADMIN role of the star-vote client will appear. Select it, and click Assign.
Go back to “Groups” screen;
Let’s create another group. So, click Create group;
Enter StarVoteUsers for the “Name” and click Create;
Click on the name of the StarVoteUsers group to select it;
Select the “Role mapping” tab and click Assign role;
Choose “Filter by clients” and type STAR in the “Search by role name” field. The STAR-VOTE-USER role of the star-vote client will appear. Select it, and click Assign.
The jwt.auth.converter.principal-attribute property configures the principal attribute. It determines how the user is identified in the JWT.
The spring.security.oauth2.resourceserver.jwt.issuer-uri property specifies the URI of the issuer for JWT tokens. The issuer URI is used to verify the authenticity and integrity of the JWT token received by the resource server;
The spring.security.oauth2.client.provider.keycloak.issuer-uri specifies the URI of the OpenID Connect (OIDC) provider (in this case, Keycloak) for the OAuth 2.0 client. This URI is used to configure the client to authenticate and interact with the OIDC provider during the OAuth 2.0 flow;
The spring.security.oauth2.client.provider.keycloak.user-name-attribute specifies the attribute in the JWT token that represents the username or user identifier. The username attribute is used to identify the user in the application;
The spring.security.oauth2.client.registration.keycloak.clientId specifies the client ID for the OAuth 2.0 client.
The spring.security.oauth2.client.registration.keycloak.clientSecret specifies the client secret for the OAuth 2.0 client;
The spring.security.oauth2.client.registration.keycloak.scope specifies the scope requested by the OAuth 2.0 client during the authorization process. Scopes define the permissions and access rights requested by the client. In this configuration, the client requests the openid, profile, and email scopes, which are commonly used to access basic user information and profile details.
Update the login.html
Let’s apply the following change (highlighted in bold) in login.html:
The th:href="@{/oauth2/authorization/okta}" is a Thymeleaf attribute used in an anchor tag (<a>) to specify the URL for the Keycloak authentication. This endpoint initiates the OAuth2 authorization flow with the chosen identity provider. When the user clicks on the corresponding link, it triggers the authentication process and redirects them to the appropriate authorization page.
Update the WebSecurityConfig class
In WebSecurityConfig class, let’s provide the implementation to the extractRoles method:
Make sure you are in the root folder of the StarVote application in the terminal. Export the KEYCLOAK_CLIENT_SECRET environment variable and set it to the client secret provided by Keycloak. Use the following command:
Once the application is started, let’s open a browser and access: http://localhost:8080.
Then, click the Login button present on the top-right of the screen and select Keycloak.
The Keycloak login page will appear.
Type app-admin for the “Username or email” field and 123 for the “Password” field.
You are in! 🎉
Feel free to add your favorite movie stars or even register as a new user on StarVote! 😃
Testing StarVote API
Once the application is started, we can submit requests to its endpoints. Let’s try to retrieve all the stars. So, open another terminal and run the following command:
curl -i http://localhost:8080/api/stars
This command will return an HTTP 401 response, as the endpoint is now secured. In order to access it, we need to provide an Bearer access token.
Let’s go ahead and request it to Keycloak:
In a terminal, let’s create the KEYCLOAK_CLIENT_SECRET environment variable and set the star-vote client secret value to it.
Looking closed the payload, we can see the “resource_access” field in the given JSON that represents an access control configuration for a resource called “star-vote”. This resource has a single role, defined as STAR-VOTE-ADMIN.
4. Let’s proceed and provide the APP_ADMIN_ACCESS_TOKEN in the next call to the /api/stars endpoint. Here is the updated cURL command: