Quick revision
The short answer
I would write a claims contract that names every required claim (iss, sub, aud, exp, scope, tenant) with its exact name, format and meaning, uses one audience value per API so tokens cannot be replayed against the wrong service, and is versioned with announced deprecation periods. Then I would make it hard to get wrong: a shared converter in the platform starter maps the claims to Spring authorities and principal fields for every service, and contract tests validate real sample tokens from the identity provider against it.
What to remember
In this answer, we defined a claims contract. It documents each required claim with name, format and meaning, gives each API one audience value so tokens cannot be replayed against the wrong service, and is versioned with deprecation periods for breaking changes. A shared converter in the platform starter maps claims to Spring authorities and principal fields for every service and rejects malformed tokens, and contract tests with real sample tokens from the identity provider keep provider and contract in step.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would write a claims contract that names every required claim (iss, sub, aud, exp, scope, tenant) with its exact name, format and meaning, uses one audience value per API so tokens cannot be replayed against the wrong service, and is versioned with announced deprecation periods. Then I would make it hard to get wrong: a shared converter in the platform starter maps the claims to Spring authorities and principal fields for every service, and contract tests validate real sample tokens from the identity provider against it.
Why this question is asked
When teams read claims differently, the result is not just messy code. It is inconsistent security: a user is an administrator in one service and not in the next, or a token for one tenant is accepted by a service that parses the tenant id in another format and finds no match to reject. Interviewers ask this to see whether we can fix an organisational problem with a technical contract and the tooling that enforces it. In this answer, we will see how the drift happens, write the contract, decide audiences, version it, build the shared converter and protect it with contract tests.
1How Claims Drift Happens
Each team integrated with the identity provider at a different time. The first team found roles in a claim called roles. A later team used a provider feature that put group memberships in groups and treated those as roles. The tenant id arrived as tenant_id in one token type, as a prefix inside sub in another, and as a URL inside a custom claim in a third. None of these teams did anything wrong locally; there was simply no written agreement.
The danger is in the edges. A service reading roles ignores groups, so a user whose admin right comes from a group is denied there but allowed elsewhere. A service comparing tenant ids as plain strings may treat t-981 and 981 as different tenants, or, worse, fail to reject a token whose tenant it cannot parse.
2Writing the Contract
The contract is a short, versioned document owned by the identity or platform team. For each claim it states the name, the JSON type, the format, the meaning, and whether it is required.
| Claim | Format | Meaning |
|---|---|---|
| iss | https URL, exact string | our identity provider; one value per environment |
| sub | UUID string | stable user or client id; never an email |
| aud | array of API ids, e.g. orders-api | the APIs this token may be used for |
| exp, iat | seconds since epoch | expiry and issue time; access tokens live 10 min |
| scope | space-separated string | granted permissions, e.g. orders.read |
| roles | array of lower-case strings | coarse roles; replaces groups |
| tenant_id | string t-<digits>, e.g. t-981 | the tenant; required for user tokens |
Notice two choices. We pick one name for roles and ask the identity provider, through its claim mappers, to emit groups-based roles under that name, so services stop reading two places. And we define the tenant id format exactly, with an example, so a parser can reject anything that does not match.
3One Audience per API
The aud claim deserves its own rule. Each API has one registered audience value, such as orders-api, and validates that its value is in the token's aud list. A client that calls two APIs gets either a token with both audiences or, better, one token per API.
The reason is replay. Without audience checks, a token issued for a low-risk reporting service is accepted by the payments service, since both trust the same issuer. If the reporting service is compromised or simply logs tokens, an attacker can use them against payments. With strict audiences, a token is only good where it was meant to go.
4Versioning and Deprecation
Claims will change, so the contract has a version, and changes follow rules. Adding an optional claim is a minor change that can happen at any time. Renaming, removing or changing the format of a claim is a major change: we publish the new version, have the provider emit both the old and new forms for a deprecation period, for example six months, and track which services still read the old form before we stop emitting it. The groups claim in our example would go through exactly that path.
5A Shared Converter in the Platform Starter
A document alone will drift again. So we implement it once, in the shared Spring Boot starter that every service uses, and services stop parsing claims themselves.
The starter also configures the audience validator from a single property, so each service only declares its own API id. Service code then writes hasRole('SUPPORT') and reads the tenant from the principal, the same way everywhere. A malformed token is rejected rather than interpreted, which closes the dangerous edge cases.
6Contract Tests With Real Tokens
Finally we test the contract where it can break: at the identity provider. A test in the starter's pipeline obtains real tokens from the provider's test environment, for a user with roles, a user with group-based roles, a client credentials client and each tenant, and checks every claim against the contract: names present, formats correct, aud values as configured. Any change to the provider's mappers that breaks the contract fails this test before it reaches production. Services, in turn, test against sample tokens from the starter, so they all exercise the same mapping.
7Follow-up questions
What if one team really needs an extra claim?
They propose it through the contract owner. If it is only useful to one service, it usually belongs in that service's own data rather than in every token. If it is broadly useful, it is added as an optional claim in a minor version.
Should the contract also cover ID tokens?
Yes, as a separate section, because ID tokens serve clients, not APIs, and carry user profile claims. Keeping them apart also reminds teams that APIs should accept only access tokens.
How do you get existing services onto the shared converter?
By making it the default in a new starter version, giving teams a migration window, and using the deprecation tracking: services still reading groups or parsing sub for tenants show up in the inventory and are helped individually.
8Conclusion
In this answer, we defined a claims contract. It documents each required claim with name, format and meaning, gives each API one audience value so tokens cannot be replayed against the wrong service, and is versioned with deprecation periods for breaking changes. A shared converter in the platform starter maps claims to Spring authorities and principal fields for every service and rejects malformed tokens, and contract tests with real sample tokens from the identity provider keep provider and contract in step.
Open with
I would treat the token format as an API with a written contract. The contract lists the required claims, iss, sub, aud, exp, scope, roles and tenant_id, each with an exact name, JSON type, format and meaning.
Then, if they want more
For example, sub is a stable UUID, never an email, roles is one array of lower-case strings, so we ask the identity provider to emit group-based roles under that same name, and the tenant id has one format like t-981 with a clear rule to reject anything else.
Each API gets exactly one audience value and validates that it is in aud, so a token issued for a reporting service cannot be replayed against payments.
The contract is versioned: adding an optional claim is a minor change, while renaming or reformatting is a major change, where the provider emits both forms during a deprecation period and we track who still reads the old one.
To stop drift, I implement it once in our shared Spring Boot starter: a converter that maps scopes and roles to Spring authorities, builds a principal with user and tenant, and rejects malformed tokens with 401.
And I add contract tests that fetch real tokens from the provider's test environment and validate every claim, so a mapper change breaks a build, not production.
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
- RFC 7519, JSON Web Token
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 "Different teams interpret access token claims differently: some read roles from "roles"...": https://scrivio-interview-library.netlify.app/spring-boot/jwt/e106b68cdb/