r/SpringBoot • u/hakdogan75 • 1d 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 PDPthe 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.
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.
1
u/sereikis 20h ago
for the GET /documents → list default, 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.
1
u/hakdogan75 18h ago
Thanks, this is a fair point and worth spelling out.
The check on
GET /documentsasks one question, whether the caller maylistthedocumentscollection. It says nothing about which documents the caller may see. If the PDP permits it, the handler runs and returns whatever the application loads. authzen-enforcer never sees those rows, so it can't filter them, and it can't make pagination totals right either.Filtering the rows needs a decision per row, and that depends on how the application and its PDP are set up. With Keycloak, each document has to exist as its own resource for the PDP to decide on it. With an attribute-based PDP, the application has to send the attributes the policy needs, such as the owner. Then there's the question of where the filter goes. Asking per row after the query keeps the rows from leaking, but the totals still count documents the caller can't see, and pages come back short. None of this is something the SDK can guarantee from the outside, so it stays with the application.
So I won't add that test to the SDK, since the SDK doesn't produce the rows it would check. What I will do is document it clearly. A
listdecision protects access to the collection endpoint and does not filter results, and filtering rows (and keeping totals consistent) is the application's responsibility.
2
u/jonenst 16h 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)