Welcome to the Spring Boot SSO with Keycloak project! This guide will help you integrate Spring Boot with Keycloak for Single Sign-On (SSO) authentication using Docker Compose. This setup allows seamless, secure login flows and provides a robust foundation for modern applications.
Ensure you have the following tools installed to run the project smoothly:
- Docker 🐋: Install Docker
- Docker Compose 🛠️: Install Docker Compose
- Java 21 ☕: Install Java
- Maven 📦 (or Gradle if preferred): Install Maven / Install Gradle
Follow these steps to get the project up and running.
First, clone the project from GitHub to your local machine:
git clone https://github.com/Dapravith/springboot-sso-keycloak.git
cd springboot-sso-keycloak
KEYCLOAK_USER=admin
KEYCLOAK_PASSWORD=admin
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
Now let's run Docker Compose to spin up the Keycloak and PostgreSQL services:
docker-compose up -d
This will start Keycloak at http://localhost:8080. Set up a PostgreSQL database (if applicable).
Navigate to http://localhost:8080/auth. Login with the admin credentials (from .env). In the Administration Console, create a new Realm or import one from the file located at /keycloak/realm-export.json. Set up clients and users for your application.
Update your Spring Boot application settings to connect with the Keycloak server. Modify the application.yml to include Keycloak OAuth2 properties:
spring:
security:
oauth2:
client:
registration:
keycloak:
client-id: your-client-id
client-secret: your-client-secret
scope: openid
provider:
keycloak:
issuer-uri: http://localhost:8080/auth/realms/{realm}/.well-known/openid-configuration
Replace your-client-id, your-client-secret, and {realm} with your actual Keycloak configurations.
You can now build and run the Spring Boot application using Gradle.
./gradlew build
./gradlew bootRun
Access your Spring Boot application at http://localhost:8090. You will be redirected to Keycloak for authentication.