r/SpringBoot • u/hakdogan75 • 13d ago
Discussion Externalized authorization for Spring Boot 4 with a single annotation, built on the OpenID AuthZEN standard
I spent the last few months digging into the OpenID AuthZEN Authorization API, which became a final specification in January 2026. It addresses a problem that anyone who has connected a Spring app to a PDP will recognize. Every PDP (Keycloak, OPA, Cerbos, Topaz, ...) comes with its own API, so the enforcement code ends up tied to one vendor. AuthZEN standardizes that API, so I built an open source Java SDK with a Spring Boot starter on top of it.
With the starter, a handler is protected like this:
@CheckPermission // read on {type: "documents", id: <id>}
@GetMapping("/documents/{id}")
Document get(@PathVariable String id) { ... }
@CheckPermission(action = "cancel", resourceType = "order", resourceId = "#id")
@PostMapping("/orders/{id}/cancel")
void cancel(@PathVariable String id) { ... }Whatever the annotation leaves out is inferred:
- the subject from the authenticated caller in Spring Security (
subclaim, typeuser) - the action from the HTTP method (
read/list,create,update,delete), while CORS preflight requests pass through without asking the PDP - the resource from the route pattern Spring matched, never from the raw URI, so the way a client spells the path cannot change what the PDP is asked about
Every inferred part can be overridden on its own, in the annotation or in configuration. Routes that cannot be inferred safely, such as /{tenant}/documents, are denied without calling the PDP and reported as warnings at startup. A deny becomes an AccessDeniedException, so your existing error handling keeps working.
The only required setting is authzen.pdp.url. The PEP authenticates to the PDP with its own credential through a CredentialProvider bean, never with the user's token, and anything that prevents a decision (timeout, 5xx, invalid response) means deny.
The starter is a preview for Spring MVC on Spring Boot 4. WebFlux support and an adapter for Spring Security's OAuth 2.0 client are next. For batch decisions, such as a bulk delete, you can inject the client and send one request yourself; the examples repository has a runnable app with a Keycloak realm that shows both.
- Repo: https://github.com/authzenenforcer/authzen-enforcer
- Article with the full background: https://hakdogan.medium.com/an-introduction-to-openid-authzen-and-authzen-enforcer-a-java-sdk-933f2122bdd9
I'd really appreciate feedback on the annotation design and the inference defaults, especially from anyone who has built something similar in-house.
2
u/jonenst 12d ago
I would love for more standards in this area. For many years I have almost felt depressed because of the fragmented support for authz in many frameworks (including springboot) :( I felt like I had to reinvent the wheel over and over again (and i'm bad at inventing wheels)
2
u/Cerbosdev 5d ago
nice work, u/hakdogan75! inferring the resource from the matched route pattern and denying /{tenant}/documents at startup instead of guessing are both good calls
on the subject defaults: some PDPs need roles sent in the request rather than looking them up. Cerbos (i work there, it's one of the PDPs in your list) reads them from subject.properties.cerbos.roles on its AuthZEN endpoint, so with only sub and type user every check goes out with no roles. a configurable mapper from Spring Security's granted authorities into subject properties would cover that without hardcoding any PDP's property names.
agree with u/sereikis that allowing list on the collection doesn't filter which documents come back, and you're right that the SDK can't fix it from outside.
one thing for the docs page though :) some PDPs can answer "which documents can this user see" with a filter instead of a yes or no. ours does it with a query plan, a condition you add to the query, so the database only returns allowed rows and pagination totals come out right. that's on our native API (client.plan(...) in the Java SDK), not AuthZEN, so it wouldn't go through your starter.
also, our CPO & co-founder Alex Olivier co-chairs the OpenID AuthZEN working group, happy to connect you if you'd like his take on the starter. feel free to dm me
1
u/sereikis 13d ago
for the
GET /documents→listdefault, add a test where the caller can read one document but not another in the same collection. allowing the list operation doesn't itself filter the returned rows; verify that both results and pagination totals respect per-document permissions, and document whether the application or PDP handles that filtering.