Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Spring resource server: 'An error occurred while attempting to decode the Jwt'

ErrorAn error occurred while attempting to decode the Jwt: Malformed Jwk set

Short 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.

  • Applies to Spring Security 6.x, Spring Boot 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Fetch the exact URI the app uses and look at the body.

    curl -i "$JWK_SET_URI"
  2. 2Match what you see, then jump to its fix

    1. 1curl shows HTML, a login redirect or JSON without a keys arrayjwk-set-uri points to a page that is not a JWK SetFix
    2. 2the URI was copied from a tutorial or guessedA hand-written jwk-set-uri is wrong; let Spring discover itFix

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/resources

2Fix

Use the key-set address your authorization server documents.

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: https://idp.example.com/.well-known/jwks.json

3Verify

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

Did this fix it?

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/resources

2Fix

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/issuer

3Verify

Restart; the app starts and a valid bearer token returns 200.

curl -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/me

Confirmed by docs.spring.io

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.