Original sources · POC — modules
PDS — README (upstream base repo)
Note (2026-11-30): this is the upstream README of the cloned base repo (IAMDevBox/keycloak-spring-boot-oauth2, MIT — see
NOTICE), kept verbatim. It describes the originaldemorealm and endpoints, not this module as modified for the POC (bqtrealm, accounts domain, MCP route, isolated DPoP filter). For this module readspec.mdandTESTING.md.
keycloak-spring-boot-oauth2
Production-ready Spring Boot 3.x OAuth2 Resource Server with Keycloak integration. Features JWT validation, Keycloak realm role extraction, multi-tenant support, and Testcontainers integration tests.
📖 Full tutorial: Keycloak Spring Boot OAuth2 Integration: Complete Developer Guide
Features
- Spring Boot 3.2+ with Spring Security 6.x OAuth2 Resource Server
- Keycloak JWT validation via JWKS auto-discovery
- Custom
KeycloakRoleConverter— extractsrealm_access.rolesinto Spring Security authorities - Client-level role extraction from
resource_access - Token audience validation (prevents cross-service token reuse)
- Multi-tenant realm support with per-realm decoder caching
- Integration tests with Testcontainers Keycloak container
- Unit tests using Spring Security's
jwt()mock - Docker Compose for local Keycloak development
Quick Start
1. Start Keycloak
docker compose -f docker/docker-compose.yml up -dThis starts Keycloak at http://localhost:8080 with a pre-configured demo realm and test users.
2. Run the Application
./mvnw spring-boot:runThe app starts at http://localhost:8081.
3. Get a Token
# Get access token (user role)
TOKEN=$(curl -s -X POST \
http://localhost:8080/realms/demo/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=password" \
-d "client_id=demo-app" \
-d "client_secret=demo-secret" \
-d "username=testuser" \
-d "password=password" | jq -r '.access_token')
# Call protected endpoint
curl -H "Authorization: Bearer $TOKEN" http://localhost:8081/api/profile4. Test Admin Endpoint
# Get admin token
ADMIN_TOKEN=$(curl -s -X POST \
http://localhost:8080/realms/demo/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=password" \
-d "client_id=demo-app" \
-d "client_secret=demo-secret" \
-d "username=adminuser" \
-d "password=password" | jq -r '.access_token')
curl -H "Authorization: Bearer $ADMIN_TOKEN" http://localhost:8081/api/admin/usersProject Structure
src/
├── main/java/com/iamdevbox/keycloak/
│ ├── config/
│ │ └── SecurityConfig.java # Security filter chain configuration
│ ├── security/
│ │ └── KeycloakRoleConverter.java # Realm role → Spring authority converter
│ └── controller/
│ └── UserController.java # Demo REST endpoints
└── test/java/com/iamdevbox/keycloak/
├── KeycloakIntegrationTest.java # Testcontainers real Keycloak test
└── UserControllerUnitTest.java # JWT mock unit testsKey Configuration
application.yml
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: http://localhost:8080/realms/demoKeycloak Role Mapping
Keycloak places roles in realm_access.roles — this is different from standard OAuth2 scope claims. The included KeycloakRoleConverter handles this automatically:
{
"realm_access": {
"roles": ["ROLE_USER", "ROLE_ADMIN"]
}
}Maps to Spring Security authorities: ROLE_USER, ROLE_ADMIN.
Testing
# Run all tests (starts Keycloak via Testcontainers)
./mvnw test
# Run unit tests only (no Keycloak required)
./mvnw test -Dgroups=unitAPI Endpoints
| Method | Path | Role Required | Description |
|---|---|---|---|
| GET | /api/profile | USER | Returns token claims for authenticated user |
| GET | /api/admin/users | ADMIN | Admin-only endpoint |
| GET | /api/token-info | Any authenticated | Returns full token metadata |
| GET | /public/health | None | Public health check |
Related IAMDevBox Resources
- 📖 Full Tutorial: Keycloak Spring Boot OAuth2 Integration
- 🐳 Keycloak Docker Compose Production Guide
- 🔐 Keycloak Complete Guide
- 🔑 OAuth 2.0 PKCE Implementation
- 🛠️ JWT Decode Tool
License
MIT License — see LICENSE file.