Secure a RESTful API in Spring with JWT by signing tokens with an RSA private key, verifying them with the matching public key, and protecting methods with role checks. With Spring Security 6 you configure a stateless SecurityFilterChain, enable @EnableMethodSecurity, and let the OAuth2 resource server module validate each bearer token. The public key can be shared with any service that needs to verify tokens, while only the issuer holds the private key.
This guide focuses on asymmetric signing, method-level authorization and refresh tokens, which are the parts a production API needs beyond a basic login. It targets Spring Boot 3.x and Java 17 or later.
With HMAC (HS256) every service that verifies a token can also forge one, because verification and signing use the same secret. With RS256 only the authorization service holds the private key. Other microservices need just the public key, which is safe to distribute, or can fetch it from a JWKS endpoint.
| Property | HS256 (shared secret) | RS256 (key pair) |
|---|---|---|
| Who can sign | Anyone with the secret | Only the private-key holder |
| Who can verify | Anyone with the secret | Anyone with the public key |
| Key rotation | Redeploy all services | Publish a new public key with a kid |
| Best for | One application | Several services |
Spring can read PEM-encoded keys directly into RSAPrivateKey and RSAPublicKey properties when they are PKCS#8 (private) and X.509 (public) files. Keep the private key in a secret manager or mounted secret, never in the repository.
A pure resource-server microservice needs only the decoder bean. The keyID lets you rotate keys: publish the new public key alongside the old one and switch the signer.
With Customizer.withDefaults() Spring maps the scope claim to SCOPE_ authorities. Use fine-grained scopes in the token and check them on methods:
@EnableMethodSecurity replaces the deprecated @EnableGlobalMethodSecurity, and @PreAuthorize is enabled by default with it. Method checks protect the service even if someone adds a new route and forgets the URL rule.
Issue a short-lived access token (about 15 minutes) with the scope claim, plus a longer-lived refresh token that you store server-side, hashed, so it can be revoked.
Store the refresh token as a random opaque string, return it in an HttpOnly cookie or the response body, and rotate it on every use. If an old refresh token is presented again, revoke the whole family.
The default decoder checks the signature and the exp and nbf claims. Add the issuer and audience so a token meant for another service is not accepted:
Yes, with two SecurityFilterChain beans ordered by @Order and matched by securityMatcher, for example /api/** for JWT and the rest for sessions.
Usually 5 to 15 minutes. Short lifetimes limit the damage of a stolen token because JWTs cannot be revoked individually without extra state.
Check the clock on both services, the iss and aud validators, and that the public key matches the private key that signed the token.
Sign tokens with an RSA private key, verify them with the public key in each service, and let the OAuth2 resource server module do the validation. Add @EnableMethodSecurity with scope-based @PreAuthorize rules, issue short-lived access tokens with revocable refresh tokens, and validate issuer and audience in addition to the signature.