Principle:A616567126 GPT WEB JAVA JWT Authentication
| Knowledge Sources | |
|---|---|
| Domains | Security, Authentication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Stateless authentication mechanism that issues signed JSON Web Tokens containing user claims, verified on each request via an interceptor with Redis-based token validation.
Description
JWT (JSON Web Token) is a compact, URL-safe token format for securely transmitting claims between parties. A JWT consists of three parts: header, payload, and signature. The server signs the token with a secret key, and clients include it in the Authorization header for subsequent requests.
This repository uses HMAC256-signed JWTs with 8-hour expiry. The token contains userId, name, and type claims. Additionally, the MD5 hash of the token is stored in Redis with a 7-day TTL, enabling server-side token invalidation (logout) — a hybrid stateless/stateful approach.
An interceptor (AuthorizationInterceptor) validates the token on every request, checking both the JWT signature and the Redis-stored MD5 hash.
Usage
Use this principle when:
- Implementing stateless authentication for a REST API
- Needing server-side token revocation capability
- Supporting multiple user types (admin, regular, temp) in the token claims
Theoretical Basis
JWT Structure:
header.payload.signature
{ "alg": "HS256", "typ": "JWT" }.{ "userId": 1, "name": "user", "type": 1, "exp": 1697184000 }.HMAC256_SIGNATURE
Hybrid Validation Flow:
# Abstract algorithm (NOT real implementation)
def create_token(user):
token = JWT.encode(
issuer="SYSTEM",
audience="SSP",
expires_at=now() + 8_HOURS,
claims={"userId": user.id, "name": user.name, "type": user.type},
key=HMAC256_SECRET
)
redis.set("token:" + user.id, md5(token), ttl=7_DAYS)
redis.set("client:" + user.id, user.is_mobile, ttl=7_DAYS)
return token
def verify_token(request):
token = request.header("token")
decoded = JWT.verify(token, HMAC256_SECRET)
redis_token = redis.get("token:" + decoded.userId)
if redis_token != md5(token):
return INVALID
return decoded.userId
Key Properties:
- HMAC256 signing: Secret key
TOKEN_KEYused for signing and verification - 8-hour expiry: Token expires 8 hours after issuance
- Redis validation: MD5 hash stored in Redis enables server-side revocation
- Client type tracking:
isMobileflag stored in Redis to route SSE vs WebSocket - URL whitelist:
CommandLineInitResource.urlListcontains allowed paths
Practical Guide
The repository implements this in JwtUtil and AuthorizationInterceptor:
JwtUtil.createToken(user)— creates JWT with HMAC256, stores MD5 in Redis (7-day TTL)JwtUtil.getUserId()— extracts userId from token in current requestJwtUtil.verifierToken()— verifies JWT signature and returns booleanAuthorizationInterceptor.preHandle()— checks URL whitelist, validates JWT + Redis token match, blocks non-admin access to /sys pathsAuthController.createLoginResult()— buildsUserAuthReswith token and user info
Related Pages
Implemented By
- Implementation:A616567126_GPT_WEB_JAVA_JwtUtil
- Implementation:A616567126_GPT_WEB_JAVA_AuthorizationInterceptor
- Implementation:A616567126_GPT_WEB_JAVA_AuthController