Sub topic 5
How signatures are verified: algorithms and JWKS
This chapter assumes you know the three-part JWT layout (header, payload, signature) from the earlier chapter on JWT anatomy. It also assumes you can read a basic Spring Boot application.yml. Here you will learn what a signature proves, which algorithms matter in practice, and how Spring Security finds the key that checks the signature. Claim checks such as iss, aud and exp come in a later chapter.
What a signature guarantees
A JWT signature is computed over the string base64url(header) + "." + base64url(payload). The resource server recomputes or verifies it with a key. A valid signature tells you two things:
- The token was produced by someone who holds the signing key.
- The header and payload have not changed since signing.
It does not tell you the token is still wanted, that it was meant for your API, or that the user still has access. Those questions belong to claim validation and revocation, which later chapters cover. A signed token is also not encrypted. Anyone who holds it can read the payload, so keep secrets out of it.
Symmetric and asymmetric algorithms
The header's alg field names the algorithm. Signed JWTs (JWS) fall into two families.
| Family | Examples | Key to sign | Key to verify | Typical use |
|---|---|---|---|---|
| Symmetric (HMAC) | HS256, HS384, HS512 | Shared secret | Same shared secret | One application that both issues and checks tokens |
| Asymmetric (RSA) | RS256, PS256 | Private key | Public key | Most identity providers |
| Asymmetric (ECDSA) | ES256, ES384 | Private key | Public key | Smaller keys and signatures than RSA |
For a resource server, asymmetric algorithms are almost always the right choice. The authorization server keeps the private key. Your API only needs the public key, so a compromised API cannot mint tokens. With HMAC, every service that can verify a token can also forge one.
Spring Security's Nimbus-based decoder accepts only RS256 by default. If your provider signs with something else, you must say so explicitly (shown below).
Why the algorithm is pinned
The alg header comes from the token, so an attacker controls it. In an algorithm confusion attack, the attacker sets alg to HS256 and signs the token with the server's public RSA key used as an HMAC secret. A naive library that trusts the header would verify with that same public key and accept the forgery. Spring avoids this by choosing the key type from its own configuration, not from the token. A decoder configured with a JWK set and RS256 rejects an HS256 token. A related case is alg: none, an unsigned token. It is never accepted by these decoders.
JWKS: publishing public keys
A JSON Web Key Set (JWKS) is a JSON document listing the public keys an issuer uses. Providers publish it at a URL, often something like https://issuer.example.com/realms/demo/protocol/openid-connect/certs.
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "2026-11-key-a",
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx...",
"e": "AQAB"
}
]
}The values of n are shortened here for readability. The fields you need to know:
ktyis the key type (RSA,EC).usesays what the key is for.sigmeans signing.algis the intended algorithm.kidis the key ID, a label that matches a token to a key.nandeare the RSA modulus and exponent. EC keys usecrv,xandyinstead.
The token's header carries the same kid:
{ "alg": "RS256", "typ": "JWT", "kid": "2026-11-key-a" }The kid lets an issuer publish several keys at once. That is what makes key rotation possible without downtime, covered in a later chapter.
The verification flow
Step by step:
- The decoder parses the header and checks that
algis in the allowed list. - It looks up a key in its cached JWK set whose
kidand type match. - If no key matches, it re-fetches the JWK set once, with rate limiting. This handles a freshly rotated key.
- It verifies the signature with the selected public key.
- Only then does it move on to claim validation.
Spring Security uses the Nimbus JOSE library for this work. The JWK set is cached in memory, so most requests need no network call to the issuer.
Telling Spring where the keys are
There are two main ways to configure this. The first is issuer-uri:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer.example.com/realms/demoSpring reads the provider's metadata at <issuer-uri>/.well-known/openid-configuration and finds jwks_uri there. Boot resolves this lazily on the first request, so the application can start even if the issuer is briefly down. A later chapter covers the iss check that issuer-uri also enables.
The second is jwk-set-uri, which names the JWKS URL directly and skips discovery:
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://issuer.example.com/realms/demo/protocol/openid-connect/certs
jws-algorithms: ES256Use jwk-set-uri when the provider has no discovery endpoint, or when your service cannot reach the discovery URL. The jws-algorithms property replaces the RS256 default. Note that jwk-set-uri alone does not validate the issuer.
Configuring the decoder in code
Properties cover most cases. When you need more control, build the JwtDecoder yourself. This example uses a remote JWK set with an ES256 issuer:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
@Configuration
public class DecoderConfig {
@Bean
JwtDecoder jwtDecoder() {
return NimbusJwtDecoder
.withJwkSetUri("https://issuer.example.com/realms/demo/protocol/openid-connect/certs")
.jwsAlgorithm(SignatureAlgorithm.ES256)
.build();
}
}If you have exactly one public key and no JWKS endpoint, load it directly:
import java.security.interfaces.RSAPublicKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
@Configuration
public class StaticKeyConfig {
@Bean
JwtDecoder jwtDecoder(
@Value("${app.security.jwt.public-key}") RSAPublicKey publicKey) {
return NimbusJwtDecoder.withPublicKey(publicKey).build();
}
}Spring Boot converts a property pointing at a PEM resource (for example classpath:public.pem) into an RSAPublicKey for you. A static key is simple, but rotating it means redeploying every API that uses it.
For HMAC, NimbusJwtDecoder.withSecretKey(secretKey) exists. Use it only when the same team controls both signing and verifying, and keep the secret in the environment, not in source control.
Failure modes
- Bad signature, tampered payload, or unexpected
alg. The decoder throws aBadJwtException, and the client gets 401 withWWW-Authenticate: Bearer error="invalid_token". - Unknown
kidafter a re-fetch. The token is rejected, also as 401. - JWKS endpoint unreachable and no cached keys. The decoder cannot verify anything. In current Spring Security versions this typically surfaces as a 500, not a 401, because it is a server-side failure and not a bad token. Watch for it in monitoring.
- Algorithm mismatch. If your provider moved from RS256 to ES256 but your config still lists RS256, every token fails. Check the
algin a token's header first when all requests suddenly return 401.
Exercise
A token's header says "alg": "HS256". Your decoder was built with NimbusJwtDecoder.withJwkSetUri(...) and the default algorithm settings. An attacker signed the token using your issuer's public RSA key as an HMAC secret. Will the token be accepted? Explain why, and say which setting you would change if your issuer really used ES256.
Show answer
No. The decoder takes its allowed algorithm from configuration (RS256 by default), not from the token header, so an HS256 token is rejected before any key is used. This blocks algorithm confusion. If the issuer used ES256, you would change the accepted algorithm to ES256, either with the jws-algorithms property or with .jwsAlgorithm(SignatureAlgorithm.ES256) on the decoder builder.