Fine-grained authorization with Keycloak
A Spring Boot restaurant-ordering API where Keycloak Authorization Services decide access per resource and HTTP method, instead of role checks spread across controllers.
- Type
- Personal project
- Year
- 2024
- Status
- Runs locally
- Stack
The problem
Single sign-on tells an API who the caller is, not what they may do. This project moves those decisions into Keycloak for a small restaurant domain: public menus, staff-managed restaurants and customer orders.
Express rules such as “anyone can read menus”, “only staff can change a restaurant” and “users can only read their own profile” without hard-coding role checks in every controller, and keep them changeable without a redeploy.
My role
Personal project. I wrote the Spring Boot API and its security configuration, and exported the Keycloak realm and Docker Compose setup so it can be recreated.
Approach
Authorization is treated as configuration, not code. Resources, scopes and policies are defined in the Keycloak realm; the API only enforces the decision. The realm is exported to JSON and started with Docker Compose, so the whole setup can be recreated from the repository.
Architecture
Spring Security runs as a stateless bearer-token resource server. Once a token is accepted, Keycloak’s policy-enforcer filter asks Keycloak whether this path and HTTP method are allowed, using the resources and policies stored in the realm.
- Browser or API client
- Keycloak
- Spring Security
- Policy enforcer
- Controllers
- PostgreSQL
Request flow
Technical decisions
- Keycloak Authorization Services
- Policies live in the identity server and can change without redeploying the API.
- HTTP method as scope
- GET, POST and PUT become separate permissions on one resource with no extra code.
- Claim information point
- Feeds request data (the URI) into policy evaluation for ownership-style rules.
- Stateless sessions
- Every request carries its own token, so instances can be added without sticky sessions.
- Docker Compose
- Keycloak and PostgreSQL start with one command, and the realm is imported from a JSON export.
Implementation
SecurityConfig adds Keycloak’s ServletPolicyEnforcerFilter after Spring’s BearerTokenAuthenticationFilter and turns sessions off. The enforcer reads a JSON file on the classpath that lists protected and public paths.
User and role operations go through the Keycloak admin client, wrapped in small resource classes, so the API manages identities through the same server that issues tokens.
The realm, clients and authorization settings are exported to keycloak/realm-export.json.
Challenges and solutions
Challenge
Role checks spread across controllers are hard to audit, and changing one needs a redeploy.
Solution
Keycloak’s ServletPolicyEnforcerFilter runs after Spring’s bearer-token filter; each path maps to a Keycloak resource and each HTTP method to a scope.
Challenge
“Users can only read their own profile” depends on the request, not just on the caller’s role.
Solution
A claim information point passes the request URI into policy evaluation, so a Keycloak policy can compare it with the caller.
Challenge
Menus and API docs must stay public while everything else is protected.
Solution
The enforcer configuration lists public paths explicitly; any path not listed is checked by Keycloak (default deny).
Security and reliability
- Default deny: any path not explicitly marked public is checked by Keycloak.
- CSRF protection is off because the API is stateless and authenticates with bearer tokens, not cookies.
- Ownership rules are Keycloak policies driven by the request URI, not controller code.
Testing
The repository contains a Spring Boot context-load test. The authorization rules themselves have no automated tests yet.
Trade-offs
Choice
Authorization decisions in Keycloak
What it costs
Protected requests depend on Keycloak’s policy evaluation, and the rules live outside the codebase where code review does not see them.
Choice
Stateless sessions with CSRF protection off
What it costs
Instances can be added without sticky sessions, but the API must not move to cookie-based authentication without turning CSRF protection back on.
Results
A runnable example of keeping authorization decisions in Keycloak rather than in application code, covering public paths, per-method permissions and a URI-based ownership check.
Lessons and next steps
Known limitations
- The client secret for the local realm is committed in policy-enforcer.json; it should come from environment configuration.
- No automated tests cover the policies, so a realm change could silently open or close a path.
What I would change next
- Move the client secret out of policy-enforcer.json into environment configuration.
- Add integration tests that call each protected path as different users, so a realm change that opens or closes a path fails the build.
Next projectMetrics for Spring microservices