Companion code for the follow-up article. The repository now holds two Maven
projects sharing one docs/ tree:
jwt-authentication/ the hand-written filter application (unchanged, moved)
oauth2-resource-server/ a resource server, a Keycloak compose, and a stub
issuer whose JWK Set can be mutated on command
The stub exists because Keycloak will not rotate a signing key at a chosen
second, report how many times its JWKS endpoint was fetched, or drop a key from
the published set on request - and the caching and rotation measurements need
all three. The Keycloak run confirms the same code path against a real issuer.
Findings captured under docs/output/, all from real runs:
* The default validator stack does not check aud. A token minted for another
service in the same realm is accepted.
* Spring Security builds its JWKSource with refreshAheadCache(false) and
rateLimited(false), overriding two of Nimbus's protective defaults, and
enables Nimbus caching only when NO Spring cache was supplied - so
supplying one removes the five-minute expiry.
* A key retired from the JWK Set stops being accepted at t+300s with the
default cache, and never with a Spring cache that has no TTL.
* 25 tokens carrying an unknown kid produce 25 JWKS fetches at the issuer,
through permitAll() endpoints included.
* A hyphenated client id in an authorities-claim-expression parses as
subtraction; the SpelEvaluationException is swallowed and logged at TRACE.
* A clientScopes key in a Keycloak realm import replaces the built-in scopes
rather than adding to them.
New docs chapters 12-18. README covers both projects. Existing docs and scripts
updated for the new paths; no docs/output/ file from the first article moved, so
links in the published article still resolve.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013f7f2XZXrQ6gW3RtZE187t
48 lines
1.6 KiB
Plaintext
48 lines
1.6 KiB
Plaintext
--------------------------------------------------------------------------
|
|
resource server profiles: stub,roles
|
|
--------------------------------------------------------------------------
|
|
Scenario: a signing key is compromised. The issuer publishes a replacement and
|
|
removes the compromised key from the JWK Set immediately. Tokens it signed are
|
|
already out there with an hour left to run.
|
|
|
|
1. A token signed with stub-key-1, one hour to live: GET /api/me -> 200
|
|
jwks fetches: 1
|
|
|
|
2. Issuer rotates to stub-key-2 and retires stub-key-1.
|
|
{
|
|
"message": "ok",
|
|
"activeKid": "stub-key-2",
|
|
"publishedKids": [
|
|
"stub-key-2"
|
|
],
|
|
"jwksFetches": 1
|
|
}
|
|
|
|
Anyone fetching /jwks.json from this moment sees only stub-key-2.
|
|
|
|
3. From here the ONLY traffic is the leaked token. Nothing carries an unknown kid,
|
|
so nothing forces a refresh. Whether the token keeps working is decided purely
|
|
by whether the cached JWK Set expires.
|
|
|
|
elapsed leaked jwksFetches
|
|
t+0s 200 1
|
|
t+30s 200 1
|
|
t+60s 200 1
|
|
t+90s 200 1
|
|
t+120s 200 1
|
|
t+150s 200 1
|
|
t+180s 200 1
|
|
t+210s 200 1
|
|
t+240s 200 1
|
|
t+270s 200 1
|
|
t+300s 401 3
|
|
t+330s 401 4
|
|
t+360s 401 5
|
|
t+390s 401 6
|
|
t+420s 401 7
|
|
t+450s 401 8
|
|
|
|
A row that flips to 401 is the cache expiring and the retired key going away.
|
|
A column of 200s is a resource server that has not noticed, and will not, until
|
|
something happens to bring it a token it cannot verify.
|