Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Design authentication and authorization for a set of JAX-RS services that are called by browsers, mobile apps, partner systems and other internal services.

I would put one identity provider in the middle and use OAuth 2 and OpenID Connect flows that suit each caller: authorization code with PKCE for browsers and mobile apps, often through a backend-for-frontend for the browser, and client credentials or mTLS for partners and internal services. Every JAX-RS service validates tokens locally (issuer, audience, scopes) and does coarse authorization with scopes or roles, while fine-grained data checks stay in the service. Internal calls use a documented choice of user token propagation, token exchange or service identity, gateways add rate limits and coarse checks without being blindly trusted, and authorization decisions are audited and keys rotated.

Why this question is asked

Four kinds of callers means four different threat models. A browser cannot keep a secret, a mobile app can be decompiled, a partner is a separate company, and an internal service may be compromised from inside. A design that treats them the same is either too weak for some or too awkward for others. Interviewers ask this question to see whether we can choose a flow per caller and still keep one consistent model in the services. In this answer, we will recap the OAuth 2 and OpenID Connect vocabulary, choose a flow for each caller, see what every service checks, decide how internal calls carry identity, place the gateway correctly, and finish with auditing and key rotation.

1A Quick Recap of OAuth 2 and OpenID Connect

OAuth 2 is a framework for getting access tokens: short-lived credentials that say a client may call an API, with scopes describing what it may do, such as orders:read. An authorization server, the identity provider, issues them. OpenID Connect adds a login layer on top: an ID token that tells the client who the user is. Our JAX-RS services are resource servers: they receive access tokens, usually JWTs, and decide whether to serve the request.

The flow is how a client obtains a token. The two that matter here are authorization code with PKCE, where a user logs in at the identity provider and the client exchanges a one-time code for tokens, with PKCE (Proof Key for Code Exchange) proving that the client that started the login is the one finishing it, and client credentials, where a machine authenticates as itself with no user.

2Choosing a Flow for Each Caller

With one identity provider, such as Keycloak, Okta, Entra ID or Auth0, every caller gets a token from the same issuer, and services need only one way to validate. The flows differ by caller.

Flows by caller
CallerFlowToken holderNotes
Browser appauth code + PKCE via a BFFBFF serverbrowser keeps only a session cookie
Mobile appauth code + PKCEapp, in secure storagesystem browser for login, refresh tokens rotated
Partner systemclient credentials with mTLS or private_key_jwtpartner backendscopes per partner, certificate-bound tokens
Internal serviceclient credentials or token exchangecalling servicesee the internal calls section

A note on the browser. A single-page app can run authorization code with PKCE itself, but then tokens live in JavaScript, where a cross-site scripting bug can steal them. The backend-for-frontend pattern, a small server-side component that does the OAuth flow, keeps tokens on the server and gives the browser only an HttpOnly, Secure, SameSite session cookie, is the stronger default for sensitive applications. Mobile apps use the system browser for login, never an embedded web view, and PKCE because they cannot keep a client secret.

3What Every Service Checks

Each JAX-RS service validates the token locally, without calling the identity provider per request: signature against the cached JWKS, issuer, audience equal to this service or API, expiry, and the scopes needed for the operation. MicroProfile JWT or a Spring Security resource server does this with configuration. Coarse authorization, whether this caller may use this operation at all, is done with scopes or roles, for example @RolesAllowed("orders:write").

Fine-grained authorization stays in the service, because only the service knows the data. A token can say alice may read orders; only the order service can check that order 42 belongs to alice's company. Putting that check in the gateway would require the gateway to know every service's data model.

Java
@GET
@Path("/orders/{id}")
@RolesAllowed("orders:read") // coarse: the token has the scope
public OrderView get(@PathParam("id") long id, @Context SecurityContext sc) {
Order o = orders.find(id).orElseThrow(NotFoundException::new);
if (!o.tenantId().equals(tenantOf(sc))) { // fine-grained: this caller's data only
throw new NotFoundException(); // 404 rather than 403, to avoid revealing the id exists
}
return OrderView.of(o);
}

Notice the choice of 404 for someone else's order: returning 403 would confirm that order 42 exists, which helps an attacker enumerate ids.

4Identity on Internal Calls

When the order service calls the inventory service on behalf of alice, there are three choices, and each fits a different call type. Propagating the user's token is simple and lets inventory see alice, but the token's audience must include inventory, which tends to make tokens valid everywhere. Token exchange (RFC 8693) asks the identity provider for a new token for inventory, still about alice, with a narrower audience and scopes; it is the cleanest model, at the cost of a call to the identity provider, usually cached. Service identity, a client credentials token or mTLS identity for the order service itself, fits background jobs and calls where the user does not matter.

The important thing is to document the choice per call type, so a reviewer can tell whether a given call should carry alice or the service.

Step throughToken exchange for an internal call
Order serviceIdentity providerInventory service
receives alice's token (aud: orders)
token exchange: subject alice, audience inventory
new token (aud: inventory, scope stock:read)
GET /stock, Bearer new token
validate iss, aud inventory, scope
stock levels

6 messages in order. Play them, or step through them one at a time.

Each service only accepts tokens issued for its own audience.

5Where the Gateway Fits

An API gateway in front of the services is a good place for rate limits per client, rejecting requests with no token or an obviously invalid one, and routing. It is not a replacement for validation in each service. A common anti-pattern is a gateway that validates the token and passes X-User-Id: alice to the services, which trust the header. If anything can reach a service without going through the gateway, a misconfigured route, a compromised pod, a developer port-forward, the header is just a string anyone can send. So services validate the token themselves, or trust a gateway-signed assertion only when the network path is protected, for example with mTLS between gateway and services.

6Auditing and Key Rotation

Every authorization decision that matters, especially denials and access to sensitive data, is logged with the subject, client id, operation, resource and result, without the token itself. These logs answer the question every incident review asks: who accessed what. Signing keys rotate on a schedule, for example every 90 days, with the new key published in the JWKS before use and the old one kept until its tokens expire. Services that cache the JWKS and refetch on an unknown kid handle rotation without restarts, and a regular rotation drill proves it.

7Follow-up questions

Why not let partners use API keys?

API keys are long-lived static secrets that cannot carry scopes or expiry well and are often leaked in logs or code. Client credentials with mTLS or a signed client assertion gives partners short-lived, scoped tokens and lets us revoke one partner without touching others. API keys can still identify a partner for rate limiting, but not authorize access to data.

Where do roles come from: the token or the service?

Coarse roles and scopes come from the token, managed in the identity provider. Data-specific permissions, such as which accounts a user manages, usually live in the service or a dedicated authorization service, because putting them in tokens makes tokens huge and stale.

How short should access tokens be?

Typically 5 to 15 minutes, with refresh tokens for user sessions. Short tokens limit the damage of a leak without needing a revocation check on every request.

8Conclusion

In this answer, we designed authentication and authorization around one identity provider using OAuth 2 and OpenID Connect: authorization code with PKCE for browsers and mobile, ideally through a backend-for-frontend for the browser, and client credentials or mTLS for partners and services. Each JAX-RS service validates tokens locally and does coarse checks with scopes or roles, keeping data-level checks inside. Internal calls use a documented choice of propagation, token exchange or service identity, the gateway handles rate limits and coarse checks without becoming a trusted header source, and decisions are audited while keys rotate regularly.

What to say in the interviewAbout 80 seconds aloud

Open with

I would centre everything on one identity provider using OAuth 2 and OpenID Connect, so every service validates tokens from one issuer the same way. Then I pick the flow per caller.

Then, if they want more

Browsers and mobile apps use authorization code with PKCE; for the browser I prefer a backend-for-frontend, so the tokens stay on the server and the browser only has an HttpOnly session cookie. Partners and internal services use client credentials, ideally bound to mTLS or a signed client assertion rather than a shared secret.

Every JAX-RS service validates the JWT locally, signature from the cached JWKS, issuer, audience, expiry and scopes, using MicroProfile JWT or the framework's resource server support, and does coarse checks with scopes or roles. Fine-grained checks, like whether this order belongs to the caller's tenant, stay in the service because only it knows the data.

For internal calls I decide per call type between propagating the user token, token exchange for a narrower audience, or the service's own identity, and I document it. The gateway does rate limiting and rejects obviously bad requests, but services never trust a user header from the gateway unless that path is protected with mTLS. Finally I audit authorization decisions and rotate signing keys on a schedule.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design authentication and authorization for a set of JAX-RS services that are called by...": https://scrivio-interview-library.netlify.app/java/jax-rs/56ddd74865/