Quick revision
The short answer
The mistakes that come up most often are not checking the audience, so tokens meant for other services are accepted; logging full tokens in request logs; using long-lived access tokens with no way to revoke them; keeping tokens in browser localStorage where any XSS can read them; and parsing and verifying tokens by hand. The fixes are an aud validator, masking the Authorization header in logs, short access tokens with revocable refresh tokens, a backend-for-frontend with HttpOnly cookies, and Spring Security's resource server with a maintained library.
What to remember
In this answer, we went through five common JWT mistakes in Spring services. Without an audience check, tokens for other services are accepted; full tokens in logs give credentials to anyone with log access; long-lived access tokens cannot be revoked; localStorage exposes tokens to XSS; and hand-written verification invites known attacks. The correct approaches are an aud validator, masked logs, short tokens with revocable refresh tokens, a BFF with HttpOnly cookies, and Spring Security's resource server.
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.
The mistakes that come up most often are not checking the audience, so tokens meant for other services are accepted; logging full tokens in request logs; using long-lived access tokens with no way to revoke them; keeping tokens in browser localStorage where any XSS can read them; and parsing and verifying tokens by hand. The fixes are an aud validator, masking the Authorization header in logs, short access tokens with revocable refresh tokens, a backend-for-frontend with HttpOnly cookies, and Spring Security's resource server with a maintained library.
Why this question is asked
JWT mistakes are rarely exotic. They are small configuration gaps that pass every functional test, because a valid user with a valid token always works. They show up only when someone uses a token where it should not work, which is exactly what an attacker does. Interviewers ask for common mistakes to see whether we have read real code with a reviewer's eye. In this answer, we will recap what a resource server should check, then go through the five mistakes one by one, each with the broken version, why it is dangerous, and the correct approach in Spring.
1A Quick Recap of What a Service Must Check
A Spring service that accepts JWTs is an OAuth2 resource server. For every request it should verify the signature with the issuer's public key, using an algorithm fixed in configuration, and then check the claims: iss (who issued the token), aud (which service the token is for), exp (when it expires) and nbf (not valid before). Only then does it turn the token's scopes or roles into authorities.
Spring Security does most of this when we configure oauth2ResourceServer with a JwtDecoder. Most of the mistakes below come from either skipping that support or from assuming it checks something it does not check by default.
2Mistake 1: Not Checking the Audience
In a company with one identity provider, every service trusts the same issuer and the same keys. If a service checks only the signature, issuer and expiry, then a token issued for the reporting service is also accepted by the payments service. A user who can get any token, or a compromised low-value service that receives tokens, can call high-value services with them.
Recent Spring Boot versions support the audiences property; on older versions we add a JwtClaimValidator for the aud claim to the decoder's validators. Check your version. The identity provider must also be configured to put the right audience in tokens, so that each client gets tokens only for the services it calls.
3Mistake 2: Logging Full Tokens
A request-logging filter that prints every header writes bearer tokens into log files, which are read by many people and shipped to log platforms with long retention. Anyone with log access can copy a token and use it until it expires. Debug logging of HTTP clients does the same for outgoing calls.
The fix is to mask or omit the Authorization header in request logs and HTTP client logs, and to log identifiers instead: the subject and the token id (jti) are enough to trace a request without exposing a usable credential. A log pipeline rule that redacts anything that looks like a bearer token is a good second layer.
4Mistake 3: Long-Lived Access Tokens
To avoid refresh logic, some teams issue access tokens that last a day or a week. Because services validate JWTs locally, such a token cannot easily be revoked: if it leaks, or the user is disabled, it keeps working until it expires. The fix is short access tokens, typically 5 to 15 minutes, with refresh tokens that the authorization server can check and revoke. For service-to-service calls, Spring's OAuth2 client fetches and renews client-credential tokens automatically, so short lifetimes cost no extra code.
5Mistake 4: Tokens in Browser localStorage
Single-page apps often store the access token, and sometimes the refresh token, in localStorage. Any JavaScript running on the page can read localStorage, so a single XSS flaw (cross-site scripting, where an attacker gets their script to run in our page), or a compromised third-party script, can steal the tokens and send them anywhere.
The safer pattern is a backend-for-frontend (BFF): a small server-side component, which in Spring can be an OAuth2 client application or Spring Cloud Gateway with token relay, performs the login, keeps the tokens on the server, and gives the browser only an HttpOnly, Secure, SameSite session cookie that JavaScript cannot read. Because that is a cookie, the BFF needs CSRF protection. XSS is still serious with a BFF, but it can no longer walk away with long-lived tokens.
6Mistake 5: Hand-Written Parsing and Verification
The last mistake is code that splits the token on dots, decodes the parts and checks the signature itself, often choosing the algorithm from the token's own header. That invites alg none tokens and algorithm-confusion attacks, and usually skips claims validation. The fix is to delete it and use oauth2ResourceServer().jwt() with NimbusJwtDecoder, which fixes the algorithm, fetches keys from the JWKS endpoint, selects them by kid and runs the standard validators.
| Mistake | Risk | Correct approach |
|---|---|---|
| No audience check | Tokens for other services accepted | audiences property or aud validator |
| Full tokens in logs | Anyone with log access can reuse them | Mask Authorization; log sub and jti |
| Day-long access tokens | Leaked tokens work for a day | 5 to 15 min + revocable refresh |
Tokens in localStorage | XSS steals them | BFF with HttpOnly cookie + CSRF |
| Hand-written verification | alg none, algorithm confusion | Resource server + NimbusJwtDecoder |
7Follow-up questions
Is it enough to check the issuer if all services are ours?
No. The issuer tells us who signed the token, not who it was meant for. Inside one company, the audience check is what stops a token for a low-risk service from being replayed against a high-risk one.
Are tokens in sessionStorage safer than in localStorage?
Only slightly. sessionStorage is cleared when the tab closes, but any script on the page can still read it, so XSS steals it just as easily. The real improvement is keeping tokens out of the browser with a BFF.
How would you find these mistakes across many services?
A short checklist in code review, a shared security starter that configures audience, algorithms and log masking by default, and automated tests that send a wrong-audience token and an expired token to each service and expect 401.
8Conclusion
In this answer, we went through five common JWT mistakes in Spring services. Without an audience check, tokens for other services are accepted; full tokens in logs give credentials to anyone with log access; long-lived access tokens cannot be revoked; localStorage exposes tokens to XSS; and hand-written verification invites known attacks. The correct approaches are an aud validator, masked logs, short tokens with revocable refresh tokens, a BFF with HttpOnly cookies, and Spring Security's resource server.
Open with
The first one I see a lot is not checking the audience. A service validates the signature and issuer but not aud, so any token from our identity provider works, including one issued for some other low-risk service.
Then, if they want more
The fix is the audiences property in Spring Boot or an aud claim validator on the decoder. The second is logging full tokens, usually from a filter that logs all headers or from HTTP client debug logging. Anyone with log access can then reuse them, so I mask the Authorization header and log the subject and token id instead.
The third is long-lived access tokens, a day or more, to avoid refresh logic. Since services validate locally, those cannot really be revoked, so I keep access tokens at five to fifteen minutes with refresh tokens the authorization server can revoke, and let Spring's OAuth2 client renew service tokens automatically.
The fourth is storing tokens in localStorage in a single-page app, where any XSS or bad third-party script can read them. I prefer a backend-for-frontend that keeps tokens on the server and gives the browser an HttpOnly SameSite cookie, with CSRF protection because it is a cookie.
And the fifth is hand-written parsing and verification, which tends to trust the alg header and skip claims. I replace it with Spring Security's resource server and NimbusJwtDecoder, and add tests that send wrong-audience and expired tokens and expect 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 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 "List common mistakes when using JWTs in a Spring service, such as those found in code...": https://scrivio-interview-library.netlify.app/spring-boot/jwt/7cbf7ee5ac/