Keycloak Setup and Configuration
Keycloak Setup and Configuration
Section titled âKeycloak Setup and ConfigurationâOverview
Section titled âOverviewâHatchgrid uses Keycloak as its identity and access management solution. Keycloak provides OAuth2/OpenID Connect capabilities, user management, and authentication flows for the application.
This document outlines:
- Local development setup
- Realm configuration
- Email configuration
- Integration with the application
Local Development Setup
Section titled âLocal Development SetupâKeycloak is configured to run as a Docker container using Docker Compose. The configuration is split between the compose file and an environment file for better portability:
services: keycloak: image: quay.io/keycloak/keycloak:${KEYCLOAK_VERSION} container_name: keycloak command: [ "start-dev", "--import-realm" ] volumes: # Absolute paths ensure correct mounting regardless of working directory - ${PWD}/infra/keycloak/realm-config:/opt/keycloak/data/import - ${PWD}/infra/keycloak/realm-config/keycloak-health-check.sh:/opt/keycloak/health-check.sh - ${PWD}/infra/keycloak/themes:/opt/keycloak/themes environment: - KC_HOSTNAME_STRICT=false - KC_HOSTNAME=${KC_HOSTNAME} - KC_HTTP_ENABLED=true - KC_DB=postgres - KC_DB_USERNAME=${POSTGRESQL_USER} - KC_DB_PASSWORD=${POSTGRES_PASSWORD} - KC_DB_URL=jdbc:postgresql://postgresql:5432/keycloak - KEYCLOAK_ADMIN=${KEYCLOAK_ADMIN} - KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD} # Additional configuration... ports: - ${KC_HTTP_PORT}:9080 - ${KC_HTTPS_PORT}:9443
Environment variables are defined in a dedicated .env
file:
KEYCLOAK_VERSION=24.0KC_HTTP_PORT=9080KC_HTTPS_PORT=9443KC_HOSTNAME=localhostKEYCLOAK_ADMIN=adminKEYCLOAK_ADMIN_PASSWORD=secretPOSTGRESQL_USER=postgresPOSTGRES_PASSWORD=postgres
Starting Keycloak
Section titled âStarting KeycloakâTo start Keycloak for local development:
# From the project root directorydocker compose up -d keycloak
This will start Keycloak with the pre-configured realm imported from infra/keycloak/realm-config/hatchgrid-realm.json
.
Important: Always run Docker Compose commands from the project root directory to ensure proper path resolution and environment variable loading.
Environment Variables
Section titled âEnvironment VariablesâKeycloak configuration uses environment variables defined in the following locations:
- Project-level
.env
file at the root directory - Keycloak-specific
.env
file ininfra/keycloak/.env
The Keycloak-specific variables include:
KEYCLOAK_VERSION
: The version of Keycloak to useKC_HTTP_PORT
: The HTTP port for Keycloak (default: 9080)KC_HTTPS_PORT
: The HTTPS port for Keycloak (default: 9443)KC_HOSTNAME
: The hostname for Keycloak (default: localhost)KEYCLOAK_ADMIN
: Admin usernameKEYCLOAK_ADMIN_PASSWORD
: Admin passwordPOSTGRESQL_USER
: PostgreSQL usernamePOSTGRES_PASSWORD
: PostgreSQL password
Accessing Keycloak Admin Console
Section titled âAccessing Keycloak Admin ConsoleâThe Keycloak admin console is available at:
http://localhost:9080/admin/
Default admin credentials are defined in the .env
file:
- Username:
admin
(or the value ofKEYCLOAK_ADMIN
) - Password:
secret
(or the value ofKEYCLOAK_ADMIN_PASSWORD
)
Realm Configuration
Section titled âRealm ConfigurationâHatchgrid uses a pre-configured realm defined in infra/keycloak/realm-config/hatchgrid-realm.json
. This realm includes:
- Client configurations
- User roles and groups
- Authentication flows
- Email templates
- Password policies
The realm is automatically imported when Keycloak starts.
Email Configuration
Section titled âEmail ConfigurationâKeycloak is configured to use GreenMail for sending emails during development. The SMTP configuration is defined in the realm configuration:
"smtpServer": { "replyToDisplayName": "Hatchgrid", "starttls": "false", "auth": "false", "envelopeFrom": "", "ssl": "false", "password": "secret", "port": "3025", "replyTo": "noreply@hatchgrid.local", "host": "greenmail", "from": "noreply@hatchgrid.local", "fromDisplayName": "Hatchgrid Development", "user": "developer"}
For more details on email testing, see Email Testing with GreenMail.
Integration with the Application
Section titled âIntegration with the ApplicationâHatchgridâs backend is configured as an OAuth2 resource server that validates JWT tokens issued by Keycloak. The integration is configured in the application properties:
spring: security: oauth2: resourceserver: jwt: issuer-uri: http://keycloak:9080/realms/hatchgrid jwk-set-uri: http://keycloak:9080/realms/hatchgrid/protocol/openid-connect/certs
For more details on the authentication flow, see Authentication Architecture.
Customizing Keycloak
Section titled âCustomizing KeycloakâCustom themes can be placed in the infra/keycloak/themes
directory. These are mounted into the Keycloak container and can be selected in the admin console.
Realm Changes
Section titled âRealm ChangesâIf you need to make changes to the realm configuration:
-
Make changes through the Keycloak admin console
-
Export the realm configuration:
Terminal window docker exec -it keycloak /opt/keycloak/bin/kc.sh export --realm hatchgrid --file /tmp/hatchgrid-realm.jsondocker cp keycloak:/tmp/hatchgrid-realm.json infra/keycloak/realm-config/hatchgrid-realm.json -
Commit the updated realm configuration file
Troubleshooting
Section titled âTroubleshootingâCommon Issues
Section titled âCommon IssuesâKeycloak Not Starting
Section titled âKeycloak Not StartingâCheck the logs:
docker compose logs keycloak
Port Binding Issues
Section titled âPort Binding IssuesâIf you see errors about ports already being in use, you can modify the port mappings in the .env
file:
KC_HTTP_PORT=9080 # Change this to an available portKC_HTTPS_PORT=9443 # Change this to an available port
Container Communication Issues
Section titled âContainer Communication IssuesâIf services canât communicate with each other, ensure that:
- The ports are not bound to 127.0.0.1 in the compose file
- The services are on the same Docker network
- The KC_HOSTNAME is set correctly in the environment variables
Email Not Being Sent
Section titled âEmail Not Being Sentâ-
Ensure GreenMail is running:
Terminal window docker compose ps greenmail -
Check GreenMail logs:
Terminal window docker compose logs greenmail -
Verify the SMTP configuration in the Keycloak admin console:
- Go to Realm Settings > Email