This tutorial explains how to create a sample Java application using Spring Boot 3 and protect it with Spring Security and Keycloak, without requiring Keycloak adapters.
- Java 17+
- Docker installed
- Spring Boot 3
- Keycloak 17
We will install Keycloak 17 using a Docker container. This will set up an instance of Keycloak where we can manage realms and clients. For this example, we will use admin
as the username and password
as the password.
$ docker pull keycloak/keycloak:17.0.0
$ docker run -p 8080:8080 \
-e KEYCLOAK_ADMIN=admin \
-e KEYCLOAK_ADMIN_PASSWORD=password \
keycloak/keycloak:17.0.0 start-dev
After logging in, we will create a new realm. A realm in Keycloak is a space where you define clients, roles, and users.
- Go to the Realm settings.
- Click on Add Realm.
- Name the realm external.
A client is an application that interacts with Keycloak to authenticate users. We will now create a client with the name external-client.
-
Navigate to Clients under the newly created realm.
-
Click Create.
-
Set the following configurations:
- Client ID:
external-client
- Enabled: On
- Client Protocol:
openid-connect
- Access Type:
Confidential
- Standard Flow Enabled: On
- Direct Access Grants Enabled: On
- Valid Redirect URIs:
http://localhost:8081/*
- Client ID:
-
Save the changes.
Now we will create a user for our external realm. This user will be used to authenticate with the Java application.
- Navigate to Users.
- Click Add User.
- Set