Masa Berlaku JWT dan Aturan Validasi
Pahami cara mengendalikan masa berlaku JWT dengan klaim berbasis waktu seperti exp, nbf, dan iat, serta cara validator menolak token yang kedaluwarsa atau digunakan terlalu dini.
Masa Berlaku JWT dan Aturan Validasi adalah pelajaran Spring Security 6 & JWT Authentication gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Security 6 & JWT Authentication, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Security 6 & JWT Authentication mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Belajar Java dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Masa Berlaku JWT dan Aturan Validasi” gratis?
Ya — teks lengkap “Masa Berlaku JWT dan Aturan Validasi” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Security 6 & JWT Authentication, upgrade ke CoddyKit PRO. Kursus Spring Security 6 & JWT Authentication mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Masa Berlaku JWT dan Aturan Validasi”?
Pahami cara mengendalikan masa berlaku JWT dengan klaim berbasis waktu seperti exp, nbf, dan iat, serta cara validator menolak token yang kedaluwarsa atau digunakan terlalu dini. Kamu berlatih Spring Security 6 & JWT Authentication dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Spring Security 6 & JWT Authentication?
Tidak diperlukan pengalaman sebelumnya. Spring Security 6 & JWT Authentication di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Masa Berlaku JWT dan Aturan Validasi” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Security 6 & JWT Authentication ini?
Ya. Setiap pelajaran Spring Security 6 & JWT Authentication menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Memahami JSON Web Tokens
- Struktur dan Klaim JWT
- Menandatangani dan Memverifikasi JWT
- Masa Berlaku JWT dan Aturan Validasi