Java and Spring Level 3 This level · 30 of 56
Hard java spring

How do you handle dynamic authorization when permissions change at runtime across services?

The 30 second answer

I keep tokens short-lived with only identity and coarse roles, and every service validates the JWT as a resource server. Fine-grained checks happen at call time, using method security that calls a permission bean. That bean reads current permissions from a central service with a short cache, so a change takes effect within the cache lifetime instead of waiting for the token to expire.

Could you say this in an interview?

Keep the token short-lived and carry only identity and coarse roles. Each service validates the JWT as an OAuth2 resource server. Fine-grained checks happen at call time with @PreAuthorize calling a bean, for example @PreAuthorize("@perms.canEdit(authentication, #id)"), and that bean reads current permissions from a central service with a short cache. A permission change then takes effect within the cache TTL, not when the token expires. Follow-up: Revoking access right away? (Publish a change event to evict caches; keep token lifetime short.) Editor's note: the central permission service with short caching is a design choice; Spring Security provides the hooks, not the architecture.

Alternate interview scenarios

"Design a multi-tenant Spring Security setup where a user has different permissions in different tenants."

Here permissions are per tenant, not global roles. So the authority check must include the tenant, not just ask whether the user is an ADMIN. I resolve the tenant from a trusted source, either a token claim or the request host or path checked against the user's memberships, never a plain header. Authorization then checks membership and role for the tenant of the resource being accessed, for example with a @PreAuthorize permission bean keyed by user and tenant. Data access also filters by tenant, so a missed check cannot leak rows. If tenants use different identity providers, Spring Security's AuthenticationManagerResolver can choose the JWT issuer for each request.

Sources (4)
  1. Spring Security: Method securitydocs.spring.io
  2. Authorize HTTP requestsdocs.spring.io
  3. OAuth 2.0 Resource Server JWTdocs.spring.io
  4. Spring Security: OAuth 2.0 resource server multitenancydocs.spring.io
Previous
Next