Spring Security 7.1 JWT authentication on Spring Boot 4.1
Runnable companion for https://ankurm.com/spring-security-7-1-jwt-authentication-guide/ - login -> token issue -> OncePerRequestFilter -> SecurityContext, end to end - HS256 and RS256 variants (RS256 publishes a real JWKS endpoint) - the same API secured by the built-in oauth2ResourceServer().jwt(), for comparison - 11 documentation chapters under docs/, interlinked with the code - docs/output/ is real captured output, regenerated by scripts/run-all.sh - 13 passing tests pinning the 401-vs-403 contract and the CSRF failure Verified against Spring Boot 4.1.1, Spring Security 7.1.1, JDK 25.0.4.1.
This commit is contained in:
45
scripts/rs256-demo.sh
Executable file
45
scripts/rs256-demo.sh
Executable file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env bash
|
||||
# Requires: --spring.profiles.active=rs256
|
||||
set -u
|
||||
BASE="${1:-http://localhost:8080}"
|
||||
show() {
|
||||
local out; out=$(curl -sS -D /tmp/.h -o /tmp/.b -w '%{http_code}' "$@")
|
||||
printf 'HTTP %s\n' "$out"
|
||||
grep -iE '^www-authenticate:' /tmp/.h | sed 's/\r$//'
|
||||
[ -s /tmp/.b ] && { python3 -m json.tool < /tmp/.b 2>/dev/null || cat /tmp/.b; echo; }
|
||||
}
|
||||
jwt_part(){ echo "$1" | cut -d. -f$(( $2 + 1 )) | tr '_-' '/+' | sed 's/$/==/' | base64 -d 2>/dev/null | python3 -m json.tool 2>/dev/null; }
|
||||
hr(){ printf '\n%s\n' "--------------------------------------------------------------------------"; }
|
||||
|
||||
echo "=========================================================================="
|
||||
echo " jwt-auth-demo - RS256 variant"
|
||||
echo " profiles: rs256 (private key signs, public key / JWKS verifies)"
|
||||
echo "=========================================================================="
|
||||
|
||||
hr; echo "# 1. The public half, published as a JWK Set. No private material here -"
|
||||
echo "# n and e only. Any number of resource servers can poll this."
|
||||
echo; show "$BASE/.well-known/jwks.json"
|
||||
|
||||
hr; echo "# 2. Login. Same endpoint, same request, different signature algorithm."
|
||||
echo; show -X POST "$BASE/api/auth/login" -H 'Content-Type: application/json' \
|
||||
-d '{"username":"root","password":"root-password"}'
|
||||
TOK=$(python3 -c "import json;print(json.load(open('/tmp/.b'))['accessToken'])")
|
||||
|
||||
hr; echo "# 3. The JOSE header now carries alg=RS256 and the kid that selects the key."
|
||||
echo; jwt_part "$TOK" 0
|
||||
|
||||
hr; echo "# 4. Token length. RS256 signatures are 256 bytes; HS256 signatures are 32."
|
||||
echo
|
||||
printf ' RS256 access token: %s characters\n' "${#TOK}"
|
||||
printf ' signature segment : %s characters\n' "$(echo "$TOK" | cut -d. -f3 | wc -c)"
|
||||
|
||||
hr; echo "# 5. It works exactly the same from the caller's side."
|
||||
echo; show "$BASE/api/me" -H "Authorization: Bearer $TOK"
|
||||
|
||||
hr; echo "# 6. Tampered payload, original signature -> 401, same as HS256."
|
||||
HDR=$(echo "$TOK" | cut -d. -f1); SIG=$(echo "$TOK" | cut -d. -f3)
|
||||
FORGED=$(echo "$TOK" | cut -d. -f2 | tr '_-' '/+' | sed 's/$/==/' | base64 -d 2>/dev/null \
|
||||
| sed 's/"sub":"root"/"sub":"mallory"/' | base64 -w0 | tr '/+' '_-' | tr -d '=')
|
||||
echo; show "$BASE/api/me" -H "Authorization: Bearer $HDR.$FORGED.$SIG"
|
||||
|
||||
hr; echo "# end"
|
||||
Reference in New Issue
Block a user