Securing Spring Boot Application with Keycloak: Access Token, Login, Logout, and Endpoint Testing

Shubhra Mukhopadhyay
4 min readJun 13, 2023

Introduction

Securing web applications requires implementing authentication and authorization mechanisms. Keycloak, an open-source identity and access management solution, provides powerful features for securing Spring Boot applications. In this blog, we will explore how to configure Keycloak within a Spring Boot application and test various functionalities like access token generation, login, logout, and endpoint authorization.

Setting Up a New Realm in Keycloak

To start, we’ll dive into Keycloak and set up a new realm called “springboot-keycloak” in the Keycloak administration console. This realm acts as a security and administrative boundary, containing users, roles, and specific configuration settings tailored to your application. We’ll configure the realm settings based on your application’s requirements, ensuring a solid foundation for securing your Spring Boot application.

Adding Users and Assigning Roles

Within the “springboot-keycloak” realm, we’ll create two users, each with their respective details. Users represent individuals who can access your application, while roles define the specific permissions and access levels granted to users. By assigning appropriate roles to each user, we can finely control their access within the application.

Realm

Obtaining Client Credentials

To establish a secure connection between the Spring Boot application and the Keycloak server, we need to obtain the client ID and client secret. These credentials serve as the application’s authentication mechanism with Keycloak. We’ll retrieve these credentials from the Keycloak administration console and ensure their secure storage for later use in the Spring Boot application’s configuration.

Client Credentials

Configuring the Spring Boot Application

To start, we’ll configure the Spring Boot application within the config package. This involves setting up the necessary beans, including the SessionAuthenticationStrategy and SessionRegistry, which are crucial for handling authentication and session management. For more we can visit https://www.keycloak.org/docs/latest/securing_apps/

SecurityConfig.java
Controller class
application.properties

Setting Up the Postman Environment

To start testing our Keycloak integration, we’ll create an environment in Postman. This environment will store the necessary variables for our API requests. Create variables such as URL_keycloak, realm, client_id, client_secret, access_token, and refresh_token, providing their initial values. These variables will be used in subsequent API requests to interact with the Keycloak server.

Testing

Logging In

In Postman, create a POST request to simulate the login process. Use the provided URL_keycloak, realm, client_id, and client_secret variables in the request. Send the request to the Keycloak server to authenticate the user. Upon successful authentication, the response will include an access token.

Log in

Retrieving the Refresh Token

After obtaining the access token, create another POST request to retrieve the refresh token. Include the access token in the request headers. By sending the request to the Keycloak server, we can obtain the refresh token for future use. Verify that the response contains the refresh token as expected.

Getting Refresh Token

Endpoint Authorization

To test endpoint authorization, create a GET request in Postman to hit a specific URL or endpoint in your Spring Boot application. Include the access token in the request headers. If the access token is valid and the user has the required role, the response will provide the expected output. On the other hand, if the access token is invalid or the user lacks the necessary role, the response will indicate “Unauthorized” with a 401 status code.

Authorized Successfully
Unauthorized

Validating Responses

In each API request, validate the responses received from the Keycloak server and your Spring Boot application. Check for the presence of the access token, refresh token, or any error messages. Ensure that the responses align with the expected behavior and adhere to the defined authorization rules.

Conclusion

Integrating Keycloak with your Spring Boot application provides a powerful authorization framework to secure your web application effectively. By setting up a new realm in Keycloak, creating users with assigned roles, and configuring the Spring Boot application with the client credentials, you establish a solid foundation for secure access control. With Keycloak’s authentication and authorization capabilities, you can confidently protect your application’s resources while ensuring the confidentiality and integrity of user data.

--

--