Quick revision
The short answer
We build the decoder with NimbusJwtDecoder.withJwkSetUri(uri).jwsAlgorithm(SignatureAlgorithm.ES256).build(), then create a JwtTimestampValidator with Duration.ofSeconds(30), a JwtIssuerValidator for https://login.example.com and a JwtClaimValidator on aud that requires billing-api, combine them with DelegatingOAuth2TokenValidator and set them with decoder.setJwtValidator(...). JwtValidators.createDefaultWithIssuer(issuer) would give the issuer and timestamp checks, but with the default 60-second skew.
What to remember
In this answer, we wrote a JwtDecoder that reads keys from a JWKS URL and accepts only ES256 through jwsAlgorithm, then combined a JwtTimestampValidator with 30 seconds of skew, a JwtIssuerValidator and a JwtClaimValidator on aud with DelegatingOAuth2TokenValidator and set them with setJwtValidator. We saw that this replaces the default validators, so the timestamp check must be included, tested each rule with locally signed tokens, and noted that JwtValidators.createDefaultWithIssuer gives issuer and timestamp checks with the 60-second default.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We build the decoder with NimbusJwtDecoder.withJwkSetUri(uri).jwsAlgorithm(SignatureAlgorithm.ES256).build(), then create a JwtTimestampValidator with Duration.ofSeconds(30), a JwtIssuerValidator for https://login.example.com and a JwtClaimValidator on aud that requires billing-api, combine them with DelegatingOAuth2TokenValidator and set them with decoder.setJwtValidator(...). JwtValidators.createDefaultWithIssuer(issuer) would give the issuer and timestamp checks, but with the default 60-second skew.
Why this question is asked
Spring Boot can configure a resource server from a few properties, but sooner or later a service needs exact control: a specific algorithm, a shorter clock skew, an audience rule. Writing the JwtDecoder bean ourselves is the way to get it, and interviewers ask for it to see whether we know the pieces and one subtle trap, that setting validators replaces the defaults. In this answer, we will look at what a JwtDecoder does, write the bean step by step, see why each validator is needed, test the result, and compare it with the shortcut helper.
1What a JwtDecoder Does
A JwtDecoder turns the bearer token string into a Jwt object or throws an exception. Spring Security's resource server calls it for every request. NimbusJwtDecoder does it in two stages: first it parses the token and verifies the signature with a key from the JWKS, then it runs an OAuth2TokenValidator<Jwt> over the claims. The first stage is configured by the builder; the second by setJwtValidator.
When we declare our own JwtDecoder bean, Spring Boot backs off and uses ours, so the properties under spring.security.oauth2.resourceserver.jwt no longer configure the decoder. Everything we need must be in the bean.
2The Decoder Bean
Here is the complete configuration for the requirements in the question.
- Parse headeralg must be ES256
- Find the key by kid in the JWKScached, refetched on unknown kid
- Verify the ES256 signature
- JwtTimestampValidatorexp and nbf, 30 s skew
- JwtIssuerValidatoriss equals https://login.example.com
- JwtClaimValidator on audcontains billing-api
- All checks passed?YesReturn the JwtNoThrow, request gets 401
7 steps in order. Play them, or step through them one at a time.
The builder covers the first three steps; setJwtValidator covers the claim checks.
jwsAlgorithm(SignatureAlgorithm.ES256) replaces the default RS256, so the decoder only accepts tokens whose header says ES256 and only uses EC keys from the JWKS. The JWKS must therefore publish an EC P-256 key; an RSA-only key set would make every token fail with no matching key.
3Why Each Validator Is There
The most important thing to notice is that setJwtValidator replaces the decoder's validators. By default, NimbusJwtDecoder runs a JwtTimestampValidator with 60 seconds of skew. If we set only the issuer and audience validators, exp would no longer be checked at all and expired tokens would be accepted forever. So the timestamp validator must be in our list, and this is also where we set the 30-second skew.
JwtTimestampValidator rejects a token whose exp is more than 30 seconds in the past, or whose nbf is more than 30 seconds in the future. A shorter skew narrows the time an expired token still works, but needs well-synchronised clocks; 30 seconds is a reasonable value with NTP.
JwtIssuerValidator compares iss with the exact string, so https://login.example.com/ with a trailing slash would fail. JwtClaimValidator takes a claim name and a predicate. Notice the type List<String>: Spring converts the aud claim to a list of strings even when the token contains a single string, so the same predicate works for "aud":"billing-api" and "aud":["billing-api","reports-api"]. The null check matters because a token without aud must be rejected, not cause an error.
4Testing the Decoder
We test each rule by creating tokens that break exactly one of them. In a unit test, we can generate an EC key pair, sign tokens with it using NimbusJwtEncoder, and build a decoder around the public key instead of the JWKS URL while reusing the same validator list.
Here token(...) is a small helper that builds claims with the issuer, audience and expiry and encodes them with a JwsHeader for ES256, and JwtConfig.validators() is the same static method the bean uses, so tests and production share one validator list. The test decoder is built from a Nimbus JWT processor with the public key, and we switch off Nimbus's own claim check so only Spring's validators, the ones we are testing, decide.
5The Shortcut and the Properties
JwtValidators.createDefaultWithIssuer(ISSUER) returns a validator with the issuer check and a timestamp check, ready to pass to setJwtValidator. It is the safe base, but its skew is the default 60 seconds, and it has no audience check. So for this question we either build the list ourselves, as above, or combine the shortcut with an audience validator and accept 60 seconds.
Without a custom bean, Spring Boot properties cover part of this: jwk-set-uri, jws-algorithms, issuer-uri and audiences. As far as we know there is no property for the clock skew, so a custom skew is a common reason to write the bean; check your version.
6Follow-up questions
Why not just use issuer-uri instead of the JWKS URL?
We could build the decoder from the issuer with JwtDecoders.fromIssuerLocation, which discovers the JWKS URL at startup. But that makes startup depend on the discovery endpoint, and here we know the JWKS URL, so withJwkSetUri is simpler and starts even if discovery is slow.
How would you accept both ES256 and RS256 during a migration?
Use the jwsAlgorithms builder method that takes a consumer of the algorithm set and add both. Remove RS256 once the issuer has finished switching, because each accepted algorithm is more surface.
Where do these validation failures show up?
The resource server returns 401 with a WWW-Authenticate header naming invalid_token and a description such as the failing claim. At DEBUG level for org.springframework.security, the logs show which validator failed, which makes the 30-second skew or the audience easy to spot.
7Conclusion
In this answer, we wrote a JwtDecoder that reads keys from a JWKS URL and accepts only ES256 through jwsAlgorithm, then combined a JwtTimestampValidator with 30 seconds of skew, a JwtIssuerValidator and a JwtClaimValidator on aud with DelegatingOAuth2TokenValidator and set them with setJwtValidator. We saw that this replaces the default validators, so the timestamp check must be included, tested each rule with locally signed tokens, and noted that JwtValidators.createDefaultWithIssuer gives issuer and timestamp checks with the 60-second default.
Open with
I would declare a JwtDecoder bean built with NimbusJwtDecoder.withJwkSetUri pointing at the JWKS URL, and call jwsAlgorithm with SignatureAlgorithm.ES256 before build, so only ES256 tokens and EC keys are accepted instead of the default RS256.
Then, if they want more
Then I create three validators. A JwtTimestampValidator with Duration.ofSeconds(30), which checks exp and nbf with thirty seconds of clock skew. A JwtIssuerValidator for https://login.example.com, which compares iss exactly.
And a JwtClaimValidator of List of String on the aud claim, with a predicate that the list is not null and contains billing-api; Spring converts aud to a list even when the token has a single string. I combine them in a DelegatingOAuth2TokenValidator and call decoder.setJwtValidator.
The important detail is that setJwtValidator replaces the defaults, so if I forgot the timestamp validator, expired tokens would be accepted. There is also a shortcut, JwtValidators.createDefaultWithIssuer, which gives issuer and timestamp validation, but with the default sixty-second skew and no audience check, so here I build the list myself.
I would test it with an EC key generated in the test, signing tokens that each break one rule and checking that only the valid ones decode.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RFC 7519, JSON Web Token
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write the configuration of a JwtDecoder bean that validates tokens from a JWKS URL...": https://scrivio-interview-library.netlify.app/spring-boot/jwt/7666305a2f/