Class JwtTokenValidator
java.lang.Object
org.apache.juneau.rest.server.auth.jwt.JwtTokenValidator
- All Implemented Interfaces:
TokenValidator
TokenValidator that validates signed JWTs against a JWKS-backed key source.
Designed to be paired with BearerTokenGuard in
juneau-rest-server. It enforces a deliberately strict default policy so a misconfigured
call site can't accidentally accept dangerous tokens:
- Algorithm allowlisting — defaults to
[RS256, ES256].HS256is rejected unless the caller explicitly opts in viaalgorithms(...)."none"(unsigned JWTs) is permanently rejected — there is no opt-in. This defeats the well-known algorithm confusion attack where a JWT is signed withHS256using the RS256 public key as the HMAC secret. - Mandatory claims —
iss,aud,exp,nbfare required by default. A token missing any of them is rejected. - Clock skew — defaults to 60 seconds tolerance for
expandnbf. Capped at 300 seconds by the builder. - JWKS caching — keys are fetched and cached for 5 minutes by default. On fetch failure
the cache continues serving past TTL with a warning logged (see
JwksCache). When a fresh-cache selection returns no keys (typically because an IdP rotated its signing key mid-TTL), the cache performs one eager out-of-band refresh before failing the request. This behavior is enabled by default (jwksEagerRefreshOnKidMiss(true)) and bounded by a per-cache cooldown (default 10 seconds) plus a single-in-flight guard. Opt out viajwksEagerRefreshOnKidMiss(false). Applies only tojwksUrl(...)-backed caches; a caller-suppliedjwkSource(...)is unaffected.
Usage
Security notes
- Never disable
iss/audvalidation. The builder enforces them. - Allowlist
HS256only when both ends of the connection are services you control AND you have a secure secret distribution mechanism. JWKS-published JWTs should be asymmetric. - Rotate JWKS keys on a schedule.
JwksCachepicks up new keys after the configured TTL.
See Also:
- Since:
- 10.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic JwtTokenValidator.Buildercreate()Static creator.static JwtTokenValidator.BuilderStatic creator with explicit bean store.Set<com.nimbusds.jose.JWSAlgorithm>Returns the allowlist of signing algorithms.Returns the audience this validator requires on tokens.Returns the configured clock-skew tolerance.Returns the issuer this validator requires on tokens.Validates the supplied bearer token and returns the associatedPrincipal.
-
Constructor Details
-
JwtTokenValidator
Constructor.- Parameters:
builder- The builder to read configuration from.
-
-
Method Details
-
create
Static creator.Routes builder construction through
BeanInstantiatorso thejwksCacheTtldefault is resolved via@Valuefrom the activeSettingschain (juneau.jwt.jwksCacheTtlproperty →PT5M).- Returns:
- A new builder.
-
create
Static creator with explicit bean store.- Parameters:
beanStore- The bean store to use for dependency injection.- Returns:
- A new builder.
-
getIssuer
Returns the issuer this validator requires on tokens.- Returns:
- The configured
issvalue.
-
getAudience
Returns the audience this validator requires on tokens.- Returns:
- The configured
audvalue.
-
getAlgorithms
Returns the allowlist of signing algorithms.- Returns:
- An unmodifiable copy of the allowlist.
-
getClockSkew
Returns the configured clock-skew tolerance.- Returns:
- The clock skew duration.
-
validate
Description copied from interface:TokenValidatorValidates the supplied bearer token and returns the associatedPrincipal.- Specified by:
validatein interfaceTokenValidator- Parameters:
token- The raw token string fromAuthorization: Bearer <token>. Nevernull .- Returns:
- The authenticated principal. Must not be
null on success. - Throws:
AuthenticationException- If the token is invalid, expired, revoked, or otherwise unacceptable.
-