Sub topic 16
Pitfalls: revocation, lifetimes, CORS, and token storage
This chapter assumes you have a working JWT resource server from the earlier chapters, with a SecurityFilterChain, a JwtDecoder that checks issuer and audience, and tests that use jwt(). The pitfalls below are design and configuration problems. A secured API can pass every test and still have them.
Revocation: a valid signature is not a valid session
A resource server validates a JWT locally. It checks the signature, exp, iss, and aud, and it never asks the authorization server whether the token is still wanted. If a user logs out, is disabled, or has a token stolen, the JWT keeps working until exp.
You have three ways to handle this:
- Keep access tokens short-lived and rely on expiry.
- Switch to opaque tokens with introspection, so each request checks current state (see the earlier chapter).
- Add a denylist check, for example of
jtivalues, as an extraOAuth2TokenValidator<Jwt>. This adds a lookup per request, which gives up part of the stateless benefit.
Lifetimes: pick a window you can live with
The token lifetime is the longest time a leaked or outdated token stays usable. It is also how long stale claims stay in effect. If you remove a user's role, old tokens still carry it.
| Choice | Effect |
|---|---|
| Access token of 5 to 15 minutes | Small exposure window. The client must refresh often. |
| Access token of hours or days | Fewer refreshes. A leak is costly and revocation is slow. |
| Long-lived refresh token | Held by the client, never sent to the resource server. Protect it more strictly and rotate it. |
Spring Security's default timestamp validator allows 60 seconds of clock skew. Keep server clocks synchronized instead of raising that value.
CORS: preflight requests carry no token
Browsers send an OPTIONS preflight request before a cross-origin call that has an Authorization header. The preflight does not include the token, so the security chain would answer it with 401. Enable Spring Security's CORS support so it answers the preflight before authentication runs.
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("https://app.example.com"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
config.setAllowedHeaders(List.of("Authorization", "Content-Type"));
config.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
// In the filter chain:
// http.cors(Customizer.withDefaults())List exact origins. Do not use "*" together with credentials. CORS only restricts browsers, and it does nothing to stop other HTTP clients. It is not an authorization mechanism.
Token storage: a client decision with server consequences
The resource server never stores tokens, but how clients store them affects your risk.
- localStorage or sessionStorage: any script on the page can read the token, so one XSS bug leaks it.
- HttpOnly, Secure, SameSite cookies: scripts cannot read them, but cookies are sent automatically, so you must handle CSRF.
- Backend-for-frontend: the browser holds only a session cookie, and a server-side component keeps the tokens and calls your API. This is the safest option for browser apps.
Whichever you choose, never log the Authorization header or put tokens in URLs. Both end up in logs and browser history.
Exercise
A user's account is disabled at 10:00. Your API accepts 30-minute JWTs and validates them locally. When can that user's existing token last be used, and name one way to shorten that window without introspection?
Show answer
Until the token's exp, which can be up to 10:30 if it was issued at 10:00. To shorten the window, issue shorter-lived access tokens, or add a validator that checks a denylist of revoked jti values.
Exercise
Your browser app gets 401 on every cross-origin call, but the same request works in curl with the token. What is the likely cause?
Show answer
The browser's preflight OPTIONS request has no Authorization header, so it is rejected before CORS headers are returned. Enable http.cors(...) with a CorsConfigurationSource bean that allows the app's origin and the Authorization header.