Sub topic 5
How signatures are verified: algorithms and JWKS
This chapter assumes you know what a JWT is (header, payload, signature) and that you have a Spring Boot app that acts as a resource server. Here you learn what the signature check does, which algorithms are common, and how the resource server finds the right key.
What the signature guarantees
The signature proves two things. The token was produced by someone who holds the signing key, and the header and payload have not changed since signing. It does not hide the contents. Anyone can decode the payload, so never put secrets in a JWT.
The resource server computes the check over the first two parts of the token, base64url(header) + "." + base64url(payload), and compares the result with the third part.
Signing algorithms
The alg header names the algorithm. Two families matter in practice.
| Algorithm | Family | Key used to sign | Key used to verify |
|---|---|---|---|
| HS256 | HMAC, symmetric | Shared secret | Same shared secret |
| RS256 | RSA, asymmetric | Private key | Public key |
| ES256 | ECDSA, asymmetric | Private key | Public key |
With a symmetric algorithm, every service that can verify a token can also forge one. With an asymmetric algorithm, only the authorization server holds the private key. Resource servers hold only the public key. For this reason, most OAuth2 providers use RS256 or ES256, and Spring's NimbusJwtDecoder defaults to RS256.
Finding the key with a JWK set
An asymmetric public key is published as a JSON Web Key (JWK). A provider publishes several keys together as a JWK Set (JWKS) at a URL. A set looks like this:
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "2026-11-key",
"n": "0vx7agoebGcQSuu...",
"e": "AQAB"
}
]
}The kid (key ID) field links a key to a token. The token header carries the same kid, so the resource server can pick the matching key from the set.
Two ways to tell Spring where the keys are
Setting issuer-uri makes Spring fetch the provider's metadata at startup and read the jwks_uri from it. This also configures issuer validation.
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer.example.com/realms/demoSetting jwk-set-uri points straight at the key set. Use it when the provider offers no metadata endpoint, or when you do not want startup to depend on a metadata call. You then set the algorithm yourself if it is not RS256.
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://issuer.example.com/realms/demo/protocol/openid-connect/certs
jws-algorithms: RS256,ES256jwk-set-uri alone does not validate the issuer. Add that check yourself, as shown in the validation chapter.
Common errors
- An
algthat is not in the configured list fails with an "unsupported algorithm" message. Add it tojws-algorithms. - A
kidmissing from the key set gives a 401. The cause is often a wrong URI or a key that was just rotated. - A startup failure with
issuer-urimeans the metadata endpoint was unreachable. The issuer in the metadata must also match the configured value exactly, including any trailing slash.
Exercise
A resource server is configured with jwk-set-uri and jws-algorithms: RS256. A token arrives with the header {"alg":"HS256","kid":"abc"}. Is it accepted if the attacker signed it with the public key text as the HMAC secret, and why?
Show answer
No. The decoder only accepts algorithms in its configured list, so HS256 is rejected before any key is used. Even if it were checked, the verification key is an RSA public key, which cannot serve as an HMAC secret in the decoder. The algorithm is set by the server, not by the token.