r/SpringBoot • • 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 (sub claim, type user)
  • 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.

I'd really appreciate feedback on the annotation design and the inference defaults, especially from anyone who has built something similar in-house.

7 Upvotes

6 comments sorted by

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)

1

u/hakdogan75 12d ago

I know that feeling well, and it's a big part of why I started this. Every framework has its own way to express authorization, and every PDP has its own API, so you end up writing the same glue again for each combination.

What I like about AuthZEN is that it standardizes the part in between. The application asks the same question in the same shape, whichever PDP answers it, so switching or adding a PDP doesn't mean rewriting the enforcement code. It doesn't solve everything (filtering lists, as the comment above shows, is still on the application), but it's a lot less wheel to reinvent.

The spec reached 1.0 recently and support is growing, so I hope it gets easier from here.

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

2

u/hakdogan75 4d ago

Thanks a lot, this is really useful feedback, and good to hear from someone at Cerbos.

On the subject defaults, you're right. Today the starter copies claims into subject properties by a dotted path, for example authzen.enforcer.subject.properties.roles=realm_access.roles, but it can't take Spring Security's granted authorities as a source, and it can't write to a nested target such as cerbos.roles. A custom SubjectResolver bean works around both, but that shouldn't be needed for something this common. I've opened an issue for a configurable mapper from granted authorities into subject properties, with nested target paths and no PDP-specific names built in: https://github.com/authzenenforcer/authzen-enforcer/issues/94

On filtering, agreed. A yes or no per document can't produce correct pages and totals. The AuthZEN Search APIs return identifiers rather than a condition for the query, so a query plan like yours is a different tool. I'll add a note to the docs that some PDPs answer "which documents can this user see" with a filter on their native API, and that this sits next to the starter rather than going through it.

And yes, I'd be glad to hear Alex's take on the starter. I'll send you a DM.

1

u/sereikis 13d 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 13d ago

Thanks, this is a fair point and worth spelling out.

The check on GET /documents asks one question, whether the caller may list the documents collection. 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 list decision protects access to the collection endpoint and does not filter results, and filtering rows (and keeping totals consistent) is the application's responsibility.