JWT 过期时间与验证规则
理解如何使用 exp、nbf 和 iat 等基于时间的声明控制 JWT 的生命周期,以及验证器如何拒绝已过期或尚未生效的令牌
JWT 过期时间与验证规则 是 CoddyKit 上的免费 Spring Security 6 & JWT Authentication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Security 6 & JWT Authentication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Tokens Need a Lifetime
A JWT is a bearer credential: whoever holds it is trusted. If a token never expired, a leaked token would grant access forever.
Time-based claims limit the damage window by making tokens valid only for a short period.
The exp Claim
The exp (expiration) claim is a Unix timestamp. After this moment the token is invalid and must be rejected.
{
'sub': 'user123',
'exp': 1717200000
}The iat Claim
The iat (issued at) claim records when the token was created. It is useful for measuring token age and for revoking all tokens issued before a certain time.
{
'sub': 'user123',
'iat': 1717196400,
'exp': 1717200000
}The nbf Claim
The nbf (not before) claim defines the earliest time a token becomes valid. A token used before its nbf time must be rejected, which is handy for tokens scheduled to activate later.
{
'sub': 'user123',
'nbf': 1717196400,
'exp': 1717200000
}Choosing an Expiration Window
Short lifetimes are safer but force frequent re-authentication. A common pattern is:
- Access token: 5-15 minutes
- Refresh token: days or weeks
The short access token limits exposure; the refresh token keeps the user logged in.
Setting exp When Issuing
When you build a token, set exp relative to now. Here a 15-minute access token is created using a JWT library.
const now = Math.floor(Date.now() / 1000);
const payload = {
sub: 'user123',
iat: now,
exp: now + 15 * 60
};Validating exp on the Server
On every request the server checks exp against the current time. Most libraries do this automatically and throw if the token is expired.
try {
const claims = verify(token, secret);
} catch (err) {
if (err.name === 'TokenExpiredError') {
// reject with 401
}
}Clock Skew
Servers do not always have perfectly synchronized clocks. A small leeway (a few seconds) prevents valid tokens from being rejected because of minor clock differences.
verify(token, secret, { clockTolerance: 5 });exp Is Not Encryption
Remember: a JWT payload is only encoded, not encrypted. The exp claim stops the server from accepting the token, but anyone can read the claims. Never put secrets in the payload.
Reacting to Expiry on the Client
When the client gets a 401 due to expiry, it should silently request a new access token using the refresh token, then retry the original request.
if (response.status === 401) {
const fresh = await refreshAccessToken();
return retryWith(fresh);
}Common Validation Mistakes
Watch out for these errors:
- Forgetting to validate exp at all
- Using milliseconds instead of seconds for the timestamp
- Setting an excessively long lifetime
- Ignoring nbf, allowing premature use
Quick Check
Test your knowledge of JWT time claims.
Recap
You learned how JWT lifetime is controlled:
expsets the expiration;iatrecords issue time;nbfsets earliest validity- Use short access tokens plus longer refresh tokens
- Allow small clock skew with leeway
- exp does not encrypt the payload
Proper expiration handling keeps stolen tokens useful for only a brief window.
用 AI 导师学习 Java — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「JWT 过期时间与验证规则」课时是免费的吗?
是的 — 「JWT 过期时间与验证规则」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Security 6 & JWT Authentication 课程的其余内容,请升级到 CoddyKit PRO。 Spring Security 6 & JWT Authentication 课程共包含 4 节课。
「JWT 过期时间与验证规则」这节课中我会学到什么?
理解如何使用 exp、nbf 和 iat 等基于时间的声明控制 JWT 的生命周期,以及验证器如何拒绝已过期或尚未生效的令牌 你通过在浏览器中直接运行的动手代码来练习 Spring Security 6 & JWT Authentication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Security 6 & JWT Authentication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Security 6 & JWT Authentication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「JWT 过期时间与验证规则」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Security 6 & JWT Authentication 课中编写并运行代码吗?
能。每节 Spring Security 6 & JWT Authentication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 了解 JSON Web Token
- JWT 结构与声明
- 签署与验证 JWT
- JWT 过期时间与验证规则