All Articles
Listยท6 min read

5 Spring Security JWT Mistakes That Get You Owned

JWTs are easy to add and easy to get dangerously wrong. Five mistakes that turn your auth layer into an open door.

Part of: Advanced Java Development

JWT authentication looks simple, which is exactly why it is so often insecure. Here are five mistakes that show up again and again in Spring Security codebases.

1. Accepting the "alg: none" Token

Some libraries will happily verify a token whose header says "alg": "none" โ€” meaning no signature at all. An attacker forges any claims they like. Always pin the expected algorithm:

JwtDecoder decoder = NimbusJwtDecoder .withPublicKey(publicKey) .signatureAlgorithm(SignatureAlgorithm.RS256) // pinned, not negotiable .build();

2. Using a Weak or Shared HMAC Secret

HS256 with the secret "secret" or a short string is brute-forceable offline. If you use symmetric signing, use a long, random, high-entropy key โ€” and never commit it. Better yet, use asymmetric RS256 so only your auth server holds the signing key.

3. Not Validating Expiry, Issuer, and Audience

A signature check alone is not enough. A valid signature on a token meant for a different service still verifies. Validate the claims:

OAuth2TokenValidator<Jwt> validators = new DelegatingOAuth2TokenValidator<>( new JwtTimestampValidator(), // exp / nbf new JwtIssuerValidator("https://auth.example.com"), audienceValidator("api://orders")); decoder.setJwtValidator(validators);

4. Putting Secrets in the Payload

A JWT is signed, not encrypted. Anyone can base64-decode the payload and read every claim. Never put passwords, PII, or internal secrets in it. Assume the client can see everything inside.

5. Having No Revocation Story

JWTs are stateless, so a stolen token is valid until it expires. If your expiry is 24 hours, that is a 24-hour window for an attacker. Keep access tokens short-lived (minutes), use refresh tokens you can revoke, and maintain a deny-list for compromised tokens.

Summary

The dangerous JWT mistakes are all about trusting the token too much: trusting its algorithm, its secret, its unchecked claims, its payload, and its lifetime. Pin the algorithm, use strong keys, validate every claim, keep secrets out, and keep tokens short-lived. Get those five right and JWTs become the secure, stateless auth they are supposed to be.

#Java#Spring Security#JWT#Security

Want to go deeper?

Advanced Java Development

View Course