Quick revision
The short answer
OAuth2 is a framework for delegated authorization: a client application obtains an access token so it can call an API on the user's behalf. OpenID Connect adds authentication on top: an ID token, a JWT that tells the client who the user is, plus a userinfo endpoint and discovery. The ID token's audience is the client, and it carries claims such as sub, iss, aud, exp, nonce and auth_time; an API must accept only access tokens issued for it, checked by audience, never ID tokens meant for the client.
What to remember
In this answer, we saw that OAuth2 gives a client an access token to call an API with delegated permission, while OpenID Connect adds authentication through an ID token, a userinfo endpoint and discovery, requested with scopes openid, profile and email. The ID token is a JWT for the client, with claims such as sub, iss, aud, exp, nonce and auth_time. An API must accept only access tokens whose audience is that API, which in Spring Boot is the audiences property.
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.
OAuth2 is a framework for delegated authorization: a client application obtains an access token so it can call an API on the user's behalf. OpenID Connect adds authentication on top: an ID token, a JWT that tells the client who the user is, plus a userinfo endpoint and discovery. The ID token's audience is the client, and it carries claims such as sub, iss, aud, exp, nonce and auth_time; an API must accept only access tokens issued for it, checked by audience, never ID tokens meant for the client.
Why this question is asked
Almost every modern login uses OAuth2 and OpenID Connect, yet the two are often mixed up, and that confusion leads to real security bugs, such as APIs that accept any token the identity provider signed. Interviewers ask this question to see whether we understand which token is for whom. In this answer, we will recap what OAuth2 solves, see what OpenID Connect adds, look inside an ID token, compare it with an access token, and see why an API must refuse ID tokens and how to enforce that in Spring.
1A Quick Recap: What OAuth2 Solves
OAuth2 answers the question: how can an application call an API on a user's behalf without learning the user's password? The user signs in at an authorization server, approves the request, and the application, called the client, receives an access token. It sends that token to the API, the resource server, in an Authorization: Bearer header. The token says what the client may do, expressed as scopes such as orders.read.
Notice what OAuth2 does not define: how the client learns who the user is. An access token is meant for the API, and its format is not even specified; it may be a JWT or an opaque random string. Before OpenID Connect, many products abused access tokens to log users in, each in its own non-standard way.
2What OpenID Connect Adds
OpenID Connect (OIDC) is a thin identity layer on OAuth2. When the client includes the scope openid in its request, the authorization server, now also an OpenID provider, returns an ID token alongside the access token. Additional scopes, profile and email, ask for standard claims such as name and email address. OIDC also defines a userinfo endpoint for fetching those claims with the access token, and a discovery document at /.well-known/openid-configuration that lists the provider's endpoints and keys, which is how Spring Boot configures itself from just an issuer URL.
9 messages in order. Play them, or step through them one at a time.
The ID token stays with the client. Only the access token travels to the API.
3Inside an ID Token
An ID token is always a signed JWT. Decoded, its payload looks like this.
Each claim has a job. iss and aud say who issued the token and for whom. sub identifies the user; it, together with iss, is the stable key to store, not the email address, which can change. exp limits its life. nonce is a random value the client sent in its login request, and checking it proves the token was issued for this login, not replayed from another. auth_time tells the client how long ago the user typed their password, which matters for step-up rules such as requiring a fresh login before changing a password.
4ID Token Versus Access Token
The two tokens look similar when both are JWTs, which is exactly why they get confused. Their purposes are different.
| Aspect | ID token | Access token |
|---|---|---|
| Defined by | OpenID Connect | OAuth2 |
| Purpose | Tell the client who the user is | Let the client call an API |
| Audience (aud) | The client id, e.g. shop-web | The API, e.g. orders-api |
| Format | Always a signed JWT | JWT or opaque string |
| Read by | The client only | The API (client treats it as opaque) |
| Contains scopes | No | Yes, e.g. orders.read |
5Why an API Must Not Accept an ID Token
An ID token says this user logged in to client shop-web. It does not say the user agreed that shop-web may call the orders API, and it carries no scopes. If the API accepted it, any client that can obtain an ID token, including an unrelated application of the same identity provider, could send it to our API and be treated as the user. The signature would verify, because the same provider signed it; only the audience reveals the problem.
So an API checks that the token's aud contains its own identifier. In Spring Boot, that is one property.
Some providers also mark the token type, for example the header typ value at+jwt for JWT access tokens in RFC 9068, and an API can require it as an extra guard. The client side has the mirror rule: it must not use the access token to learn who the user is, since that token is not meant for it; it uses the validated ID token or the userinfo endpoint.
6Follow-up questions
Why not just call the userinfo endpoint instead of using the ID token?
We can, and some clients do for fresh profile data. But the ID token is signed and can be validated locally without a network call, and it carries login-specific information such as nonce and auth_time that userinfo does not.
Does Spring validate the nonce for me?
Yes. With oauth2Login(), Spring Security generates the nonce, stores it with the authorization request and checks it in the returned ID token, along with issuer, audience, expiry and signature.
Can an access token be opaque, and how does the API validate it then?
Yes. The API sends it to the authorization server's introspection endpoint, which says whether it is active and returns its scopes and subject. Spring Security supports this with oauth2ResourceServer(o -> o.opaqueToken(...)).
7Conclusion
In this answer, we saw that OAuth2 gives a client an access token to call an API with delegated permission, while OpenID Connect adds authentication through an ID token, a userinfo endpoint and discovery, requested with scopes openid, profile and email. The ID token is a JWT for the client, with claims such as sub, iss, aud, exp, nonce and auth_time. An API must accept only access tokens whose audience is that API, which in Spring Boot is the audiences property.
Open with
OAuth2 is about delegated authorization. A client application gets an access token from the authorization server so it can call an API on the user's behalf, limited by scopes, without knowing the user's password.
Then, if they want more
It does not define how the client learns who the user is. OpenID Connect adds that authentication layer: when the client asks for the openid scope, it also gets an ID token, which is always a signed JWT, and OIDC adds a userinfo endpoint, standard scopes like profile and email, and a discovery document.
The ID token is meant for the client: its aud is the client id, and it carries sub as the stable user id, iss, exp, iat, nonce to tie it to this login request, and auth_time for when the user actually authenticated.
An API should not accept it because it was never issued for the API: it has no scopes, and any other client of the same provider could present its own ID token and pass a signature check.
So the API accepts only access tokens issued for it, checked by audience; in Spring Boot I set the resource server audiences property to the API's identifier, and the ID token is rejected with 401.
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 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
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 "What is the difference between OAuth2 and OpenID Connect? What does the ID token contain...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/ab685051c0/