Add OAuth2 resource server project: JWT validation, JWKS and key rotation
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
This commit is contained in:
34
oauth2-resource-server/docker/compose.yaml
Normal file
34
oauth2-resource-server/docker/compose.yaml
Normal file
@@ -0,0 +1,34 @@
|
||||
# A real issuer, in one command.
|
||||
#
|
||||
# docker compose -f docker/compose.yaml up -d
|
||||
# ./scripts/run-rs.sh keycloak,roles
|
||||
# ./scripts/keycloak-demo.sh
|
||||
#
|
||||
# Notes that matter for a resource server:
|
||||
#
|
||||
# * KC_HOSTNAME fixes the issuer string. Keycloak derives `iss` from the request host
|
||||
# unless you pin it, so a token fetched via localhost and a token fetched via a
|
||||
# container name carry DIFFERENT issuers and one of them will fail JwtIssuerValidator.
|
||||
# Pinning it is the single most common fix for "it works from curl but not from the app".
|
||||
#
|
||||
# * start-dev keeps everything in an in-memory H2 database. Every restart is a fresh realm
|
||||
# and, importantly for this repository, a fresh signing key.
|
||||
#
|
||||
# * The realm is imported at boot from realm-demo.json, so the demo users, roles and the
|
||||
# audience mapper exist without any admin-console clicking.
|
||||
services:
|
||||
keycloak:
|
||||
image: quay.io/keycloak/keycloak:26.7.2
|
||||
container_name: jwt-demo-keycloak
|
||||
command: ["start-dev", "--import-realm"]
|
||||
environment:
|
||||
KC_BOOTSTRAP_ADMIN_USERNAME: admin
|
||||
KC_BOOTSTRAP_ADMIN_PASSWORD: admin
|
||||
KC_HOSTNAME: http://localhost:8080
|
||||
KC_HOSTNAME_STRICT: "false"
|
||||
KC_HTTP_ENABLED: "true"
|
||||
KC_HEALTH_ENABLED: "true"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- ./realm-demo.json:/opt/keycloak/data/import/realm-demo.json:ro
|
||||
129
oauth2-resource-server/docker/realm-demo.json
Normal file
129
oauth2-resource-server/docker/realm-demo.json
Normal file
@@ -0,0 +1,129 @@
|
||||
{
|
||||
"realm": "demo",
|
||||
"enabled": true,
|
||||
"sslRequired": "none",
|
||||
"accessTokenLifespan": 300,
|
||||
"roles": {
|
||||
"realm": [
|
||||
{
|
||||
"name": "USER",
|
||||
"description": "Ordinary user"
|
||||
},
|
||||
{
|
||||
"name": "ADMIN",
|
||||
"description": "Administrator"
|
||||
}
|
||||
],
|
||||
"client": {
|
||||
"reports-api": [
|
||||
{
|
||||
"name": "reports-reader",
|
||||
"description": "May read reports"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"clients": [
|
||||
{
|
||||
"clientId": "reports-api",
|
||||
"enabled": true,
|
||||
"bearerOnly": true,
|
||||
"protocol": "openid-connect",
|
||||
"description": "The resource server. It never logs anyone in; it only owns roles and is an audience."
|
||||
},
|
||||
{
|
||||
"clientId": "demo-client",
|
||||
"enabled": true,
|
||||
"publicClient": false,
|
||||
"secret": "demo-secret",
|
||||
"standardFlowEnabled": false,
|
||||
"directAccessGrantsEnabled": true,
|
||||
"serviceAccountsEnabled": false,
|
||||
"protocol": "openid-connect",
|
||||
"fullScopeAllowed": true,
|
||||
"protocolMappers": [
|
||||
{
|
||||
"name": "reports-api-audience",
|
||||
"protocol": "openid-connect",
|
||||
"protocolMapper": "oidc-audience-mapper",
|
||||
"consentRequired": false,
|
||||
"config": {
|
||||
"included.client.audience": "reports-api",
|
||||
"id.token.claim": "false",
|
||||
"access.token.claim": "true"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"users": [
|
||||
{
|
||||
"username": "alice",
|
||||
"enabled": true,
|
||||
"emailVerified": true,
|
||||
"firstName": "Alice",
|
||||
"lastName": "Anderson",
|
||||
"email": "alice@example.com",
|
||||
"requiredActions": [],
|
||||
"credentials": [
|
||||
{
|
||||
"type": "password",
|
||||
"value": "alice-password",
|
||||
"temporary": false
|
||||
}
|
||||
],
|
||||
"realmRoles": [
|
||||
"USER"
|
||||
],
|
||||
"clientRoles": {
|
||||
"reports-api": [
|
||||
"reports-reader"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"username": "root",
|
||||
"enabled": true,
|
||||
"emailVerified": true,
|
||||
"firstName": "Root",
|
||||
"lastName": "Admin",
|
||||
"email": "root@example.com",
|
||||
"requiredActions": [],
|
||||
"credentials": [
|
||||
{
|
||||
"type": "password",
|
||||
"value": "root-password",
|
||||
"temporary": false
|
||||
}
|
||||
],
|
||||
"realmRoles": [
|
||||
"USER",
|
||||
"ADMIN"
|
||||
],
|
||||
"clientRoles": {
|
||||
"reports-api": [
|
||||
"reports-reader"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"username": "nobody",
|
||||
"enabled": true,
|
||||
"emailVerified": true,
|
||||
"firstName": "No",
|
||||
"lastName": "Body",
|
||||
"email": "nobody@example.com",
|
||||
"requiredActions": [],
|
||||
"credentials": [
|
||||
{
|
||||
"type": "password",
|
||||
"value": "nobody-password",
|
||||
"temporary": false
|
||||
}
|
||||
],
|
||||
"realmRoles": [
|
||||
"USER"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user