Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 39 ยท JWT and OAuth2 Resource Server

Refresh Tokens, Rotation and Revocation

Design short-lived access tokens with refresh tokens: rotation, reuse detection, a jti denylist for logout, and where to store tokens safely in browsers and apps.

A JWT cannot be "deleted": any server with the key will accept it until exp. That is a problem for logout, stolen devices and compromised accounts. The standard answer is a two-token design: a short-lived access token (minutes) that the API checks on every request, and a long-lived refresh token that can only be exchanged at the authorization server for a new access token. The refresh token lives in a database, so it can be revoked.

Spring Authorization Server and most identity providers implement this for you. Your own code usually touches only the client side. The runnable examples below are plain Java stand-ins (the real Spring classes cannot run on this site) that model the server-side rules so you can see how rotation and revocation work.

The two-token flow

sequenceDiagram
    participant C as Client app
    participant A as Authorization server
    participant R as Resource server
    C->>A: Log in
    A-->>C: access token (10 min) + refresh token (30 days)
    C->>R: GET /api with access token
    R-->>C: 200
    Note over C,R: 10 minutes later the access token expires
    C->>R: GET /api with expired token
    R-->>C: 401
    C->>A: POST refresh token
    A-->>C: NEW access token + NEW refresh token

The refresh token is sent only to the authorization server, never to the API.

Why this helps:

  • A leaked access token is only dangerous for a few minutes.
  • The refresh token is sent rarely and to one place only, so it is harder to steal.
  • The server can revoke a refresh token to end a session in one step.

Rotation and reuse detection

Rotation means every refresh produces a new refresh token and invalidates the old one. If an attacker steals a refresh token and uses it, the real user's next refresh presents an already-used token. The server notices the reuse, concludes the token family is compromised and revokes the whole family so both parties must log in again.

Java Refresh rotation with reuse detection (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The attacker's replay of rt-1 triggers the alarm, and even Alice's valid-looking rt-2 stops working. That is deliberate: the server cannot tell who is who, so it ends the session safely.

Revoking access tokens before they expire

Usually you simply wait the few minutes. When you cannot (a banned user, a stolen laptop) keep a denylist of token ids (jti) until they would have expired anyway. The list stays tiny because entries disappear after exp.

Java jti denylist with automatic cleanup (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

In production the denylist lives in Redis with a TTL equal to the remaining lifetime, and the resource server checks it through a custom OAuth2TokenValidator<Jwt>:

Java
OAuth2TokenValidator<Jwt> notRevoked = jwt -> denylist.contains(jwt.getId())
    ? OAuth2TokenValidatorResult.failure(new OAuth2Error("invalid_token", "Token revoked", null))
    : OAuth2TokenValidatorResult.success();

The catch: every request now consults shared state, which gives back part of what stateless JWTs saved you. Use a denylist only where instant revocation is really required.

Where to store tokens

Place Risk Advice
localStorage / sessionStorage Readable by any script on the page (XSS) Avoid for refresh tokens
HttpOnly, Secure, SameSite cookie Not readable by scripts; needs CSRF protection Best for browser refresh tokens
Memory only (a JS variable) Lost on reload; safe from storage theft Good for access tokens in SPAs
OS keychain / Keystore Protected by the device Right choice for mobile apps

A common browser pattern is the backend for frontend: the Spring app holds the tokens server-side, the browser gets only a normal session cookie, and the BFF forwards API calls. It removes tokens from the browser altogether.

Refresh tokens are powerful: protect them like passwords. Bind them to a client, store only a hash in the database, and give them an absolute lifetime as well as an idle timeout.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use a short-lived access token plus a refresh token?

    Short life limits damage from theft. The refresh token is stored server-side and can be revoked.
  2. 2. What does refresh token rotation detect?

    Each refresh burns the old token. Presenting a burned token means two parties hold it, so the family is revoked.
  3. 3. How long must a jti stay in a denylist?

    After exp the token is rejected by the expiry check, so the entry can be removed.
  4. 4. Which storage is generally best for a refresh token in a browser?

    Scripts cannot read an HttpOnly cookie, which blunts XSS theft. Pair it with CSRF protection.

Summary

  • Short access tokens plus revocable refresh tokens give both safety and long sessions.
  • Rotate refresh tokens and revoke the whole family on reuse.
  • Use a jti denylist only when you need instant revocation; entries can expire with the token.
  • Keep refresh tokens out of script-readable storage; a backend for frontend removes tokens from the browser entirely.

Next: OAuth2 flows overview