Spring resource server: 'An error occurred while attempting to decode the Jwt'
An error occurred while attempting to decode the Jwt: Malformed Jwk setShort answer
The resource server downloaded something from jwk-set-uri that is not a valid JWK Set. A JWK Set is a JSON object with a top-level keys array. Open the URI in a browser or curl; if it is not that JSON, correct the URI or use issuer-uri so Spring discovers the right address.
Fix it fast
1Find out which cause you have
Fetch the exact URI the app uses and look at the body.
curl -i "$JWK_SET_URI"2Match what you see, then jump to its fix
What does the error look like?
An error occurred while attempting to decode the Jwt: Malformed Jwk set
Cause 1 of 2
jwk-set-uri points to a page that is not a JWK Set
1Check
RFC 7517 requires a JWK Set to be a JSON object with a keys member holding an array. Compare the response with that shape. Spring's docs note the JWK Set URI is not standardized, so it must come from your authorization server's documentation.
curl -s "$JWK_SET_URI" | jq 'has("keys")'
grep -rn "jwk-set-uri" src/main/resources2Fix
Use the key-set address your authorization server documents.
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://idp.example.com/.well-known/jwks.json3Verify
curl returns JSON with a keys array, and the API accepts a valid token.
curl -s "$JWK_SET_URI" | jq '.keys | length'Confirmed by datatracker.ietf.org
Cause 2 of 2
A hand-written jwk-set-uri is wrong; let Spring discover it
1Check
With issuer-uri alone, Resource Server uses the provider's metadata endpoint to find the JWK Set URI, so there is no address for you to get wrong.
grep -rn "issuer-uri\|jwk-set-uri" src/main/resources2Fix
Set issuer-uri to the value of the iss claim your tokens carry and remove the guessed jwk-set-uri.
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://idp.example.com/issuer3Verify
Restart; the app starts and a valid bearer token returns 200.
curl -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/meConfirmed by docs.spring.io
How do I stop it coming back?
- Copy the JWKS address from the authorization server's own discovery document.
- Keep jwk-set-uri in config per environment and test it with curl after changes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Security
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fixSecurity
Jwts.parserBuilder() no longer compiles after upgrading JJWT
setSigningKey / parseClaimsJws / getBody shown as deprecated
JJWT 0.12.0 removed Jwts.parserBuilder() because Jwts.parser() now returns the builder itself.
3 causes, each with a fixSecurity
Spring Security rejects the Entra ID token: invalid claims
Error occurred while attempting to decode the Jwt. The ID Token contains invalid claims
Spring checks the ID token's claims against your client registration.
3 causes, each with a fixSecurity
Spring Boot cannot find a ClientRegistrationRepository bean for OAuth2 login
APPLICATION FAILED TO START
Boot creates the ClientRegistrationRepository from spring.security.oauth2.client.registration properties.
3 causes, each with a fixSecurity
Spring Security fails to resolve the issuer configuration for a JWT or Keycloak
The identity server is up but the Spring app fails on the first token
Spring Security fetches the issuer's discovery document before it can validate tokens.
3 causes, each with a fixSecurity
Spring Security cannot verify the CSRF token because the session is gone
InvalidCsrfTokenException
By default Spring Security keeps the expected CSRF token in the HTTP session.
3 causes, each with a fix