Mastering JWT: Standard Claims, Custom Tokens, and Renewal Strategies
This article explains JWT payload claims, lists standard and custom claims, shows how to generate a token with expiration in Java, and compares single‑token and double‑token renewal strategies—including OAuth 2.0 approaches and Redis‑based storage—to manage token expiration securely.
JWT token payload is a JSON string containing claims that convey data.
Standard JWT claims include:
iss (Issuer): token issuer.
sub (Subject): token owner.
aud (Audience): token audience.
exp (Expiration time): token expiry.
nbf (Not Before): when token becomes valid.
iat (Issued At): token issuance time.
jti (JWT ID): unique identifier.
Custom claims can be added, e.g., using com.auth0 to create a token with expiration:
String token = JWT.create()
.withIssuer(ISSUER)
.withIssuedAt(new Date(currentTime)) // issuance time
.withExpiresAt(new Date(currentTime + EXPIRES_IN * 1000 * 60)) // expiration timestamp
.withClaim("username", username) // custom claim
.sign(Algorithm.HMAC256(user.getPassword()));After setting an expiration, the token becomes invalid and the user must obtain a new token, which can be automated via renewal strategies.
Single‑token renewal scheme
Set token expiration (e.g., 15 minutes).
Frontend requests; backend checks expiration.
If expired, frontend requests a refreshed token; backend issues a new token.
Frontend retries the original request with the new token.
Optionally enforce re‑login after a fixed period (e.g., 72 hours) or limit refresh count (e.g., 50 times).
Double‑token scheme
On login, backend returns an access_token and a refresh_token.
Use access_token for API calls; if it expires, use refresh_token to obtain a new access_token.
Backend validates refresh_token expiration; if valid, issues new access_token, otherwise forces re‑login.
Logout or password change invalidates both tokens.
WeChat web authorization also follows an OAuth 2.0 double‑token pattern, issuing a short‑lived access_token and a long‑lived refresh_token.
Tokens can be stored in Redis with an expiration key; absence of the key indicates token expiry.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
