The configuration is not secure and is intended for local development use only.
This setup runs Keycloak using Docker Compose alongside PostgreSQL and pgAdmin.
DockerComposeWithPgAdminPostgresqlKeycloak.yml
- PostgreSQL 16: Database for both Keycloak and the application.
- pgAdmin: Web-based UI for managing PostgreSQL.
- Keycloak: Identity and Access Management server.
Keycloak in this setup requires a PFX (PKCS#12) file for HTTPS/TLS.
- Generate the file (example using OpenSSL):
# Generate certificate and key openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem -subj "/CN=localhost" # Export to PFX (password must be 'password') openssl pkcs12 -export -out keycloak.pfx -inkey key.pem -in certificate.pem -passout pass:password
- Place the file: Copy the generated
keycloak.pfxto the same directory as theDockerComposeWithPgAdminPostgresqlKeycloak.ymlfile.
The Docker Compose file uses an external network named local-development-network. Create it if it doesn't exist:
docker network create local-development-network-
Start the containers:
docker compose -f DockerComposeWithPgAdminPostgresqlKeycloak.yml up -d
-
Create Databases: Connect to the PostgreSQL server using pgAdmin or a CLI.
- pgAdmin Credentials:
admin@development.com/password - DB Credentials:
postgres/password
Run the following SQL commands:
create database keycloak with owner postgres; create database spring_keycloak with owner postgres;
- pgAdmin Credentials:
-
Restart Keycloak: Keycloak may fail to start initially if the database was missing. Restart it:
docker compose -f DockerComposeWithPgAdminPostgresqlKeycloak.yml restart keycloak
- Access Keycloak and login with
admin/password. - Go to the Master realm dropdown (top-left) and click Create Realm:
- Realm name:
development - Click Create.
- Realm name:
- Go to Clients (left menu):
- Create
api-client:- Click Create client.
- Client ID:
api-client - Click Next until saved.
- Create
spa-client:- Click Create client.
- Client ID:
spa-client - Click Next.
- Ensure Standard flow and Direct access grants are ON.
- Click Save.
- In Settings > Access settings:
- Valid redirect URIs:
/* - Web origins:
/*
- Valid redirect URIs:
- Click Save.
- Create
- Go to Users (left menu):
- Create Admin User:
- Click Add user.
- Username:
admin - Email:
admin@development.com - Email verified: Yes.
- Click Create.
- Go to Credentials tab -> Set Password ->
password(Temporary: Off).
- Create API User:
- Click Add user.
- Username:
api.user - Email:
api.user@development.com - Email verified: Yes.
- Click Create.
- Go to Credentials tab -> Set Password ->
password(Temporary: Off).
- Create Admin User:
Use the following environment variables to configure the spring-keycloak application:
KEYCLOAK_AUTH_SERVER_URL=http://localhost:9000
KEYCLOAK_REALM=development
SERVER_PORT=8081
SPRING_APPLICATION_NAME=spring-keycloak
SPRING_DATASOURCE_PASSWORD=password
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/spring_keycloak
SPRING_DATASOURCE_USERNAME=postgres
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI=http://localhost:9000/realms/development
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI=http://localhost:9000/realms/development/protocol/openid-connect/certsGood Luck!