Security· 45 min

JWT, OAuth2, and API Security

Implement secure token flows and protect APIs against common web security mistakes.

Java interview lesson
Question 26 · Not reviewed

Security · tokens and threats

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

JWT, OAuth2, and API Security

Explain it aloud in your own words

Strong answer

OAuth2 is a delegation framework: an authorization server issues access tokens, a client obtains them (Authorization Code with PKCE for user-facing apps, Client Credentials for service-to-service), and a resource server validates them before authorizing a request. A JWT is one token format; it is trustworthy only after signature, issuer, audience, expiry, and scopes are verified. API security also covers transport, rate limits, input validation, secrets handling, and the OWASP API risks such as broken object-level authorization.

Key points

  • Use short-lived access tokens and rotate refresh tokens; JWTs cannot be revoked without extra state.
  • Pin the expected algorithm and fetch signing keys from the issuer's JWKS endpoint.
  • OpenID Connect adds an ID token for login; OAuth2 alone is about access, not identity.
  • Never put secrets or personal data in a JWT payload; it is only Base64URL-encoded, not encrypted.

Interviewer looks for

Pick the right grant for a scenario, explain exactly what the resource server checks, and discuss revocation, token storage in browsers, and how you would rotate signing keys.

Java example

1# application.yml (resource server)
2spring.security.oauth2.resourceserver.jwt:
3  issuer-uri: https://auth.example.com/realms/shop
4  audiences: orders-api
5
6@Bean
7JwtAuthenticationConverter jwtAuthConverter() {
8  var scopes = new JwtGrantedAuthoritiesConverter();
9  scopes.setAuthorityPrefix("SCOPE_");
10  var converter = new JwtAuthenticationConverter();
11  converter.setJwtGrantedAuthoritiesConverter(scopes);
12  return converter;
13}
14
15@PreAuthorize("hasAuthority('SCOPE_orders:write')")
16@PostMapping("/api/orders")
17OrderResponse create(@Valid @RequestBody CreateOrder cmd, @AuthenticationPrincipal Jwt jwt) {
18  return service.create(cmd, jwt.getSubject());
19}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

Which OAuth2 grant should a single-page app or mobile app use?

Strong answer

Authorization Code with PKCE. The app cannot keep a client secret, so PKCE binds the authorization code to a one-time verifier. The implicit grant and password grant are deprecated because they expose tokens or credentials.

Q2Intermediate

What must a resource server verify on an incoming JWT?

Strong answer

The signature using the expected algorithm and a key from the issuer's JWKS, the issuer, the audience intended for this API, expiry and not-before times, and the scopes or claims required for the endpoint. Only then does it build an Authentication and run authorization.

Q3Advanced

How do you handle logout or revocation with stateless JWTs?

Strong answer

Keep access tokens short-lived, revoke or rotate the refresh token server-side, and for high-risk cases maintain a denylist of token ids or use opaque tokens with introspection. Each option trades statelessness against immediate revocation.

Q4Intermediate

What is the difference between OAuth2 and OpenID Connect?

Strong answer

OAuth2 lets a client obtain an access token to call an API on a user's behalf. OpenID Connect is an identity layer on top that adds an ID token, standard user claims, and a userinfo endpoint so the client can log the user in.

Q5Advanced

What is broken object-level authorization and how do you prevent it?

Strong answer

It is when an API trusts an object id from the request without checking that the caller may access that object, for example GET /orders/42 returning someone else's order. Prevent it with ownership checks on every lookup, owner-scoped queries, and tests that try cross-tenant access.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.