Tokeny odświeżania i wygasanie
Rotuj tokeny dostępu bez ponownego logowania.
Tokeny odświeżania i wygasanie to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Tokens Expire
A stolen token is dangerous only while it works. Giving every access token a short expiry shrinks that window of risk. ⏳
The exp Claim
Expiry lives in the token's exp claim, a timestamp. Once the clock passes it, the token is rejected no matter what.
Set the Lifetime
Control how long access tokens last with JWT_ACCESS_TOKEN_EXPIRES. A short span like 15 minutes is a sensible default.
from datetime import timedelta
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(minutes=15)The Re-Login Problem
Short expiry is safe but annoying if users must type their password every 15 minutes. The refresh token solves exactly this.
Two Tokens, Two Jobs
A short access token calls your API; a long-lived refresh token does nothing but request fresh access tokens.
Issue Both at Login
At login, mint an access token and a refresh token together, then return both to the client.
from flask_jwt_extended import create_refresh_token
rt = create_refresh_token(identity=user.id)A Refresh Endpoint
Add a /refresh route guarded by jwt_required(refresh=True) so only a valid refresh token can reach it.
@app.post("/refresh")
@jwt_required(refresh=True)
def refresh():
...Hand Back a New Access Token
Inside /refresh, read the identity and mint a brand-new access token. The user keeps going without retyping a password. 🔄
new = create_access_token(identity=get_jwt_identity())
return {"access_token": new}Refresh Tokens Live Longer
Set JWT_REFRESH_TOKEN_EXPIRES to days or weeks. It is exposed less often, so a longer life is an acceptable trade.
app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta(days=30)Store the Refresh Token Safely
Because it is powerful, keep the refresh token in secure storage, never in plain JavaScript-readable space, and send it only to /refresh.
Revoking Tokens
To truly log someone out, add a blocklist of token ids the server refuses. This is the one bit of state stateless auth sometimes needs.
Quick Check
Recall the job each token type does.
Recap
Keep access tokens short and pair them with a long-lived refresh token that buys new ones at /refresh. Add a blocklist to revoke. ✅
Często zadawane pytania
Czy lekcja „Tokeny odświeżania i wygasanie” jest bezpłatna?
Tak — pełny tekst „Tokeny odświeżania i wygasanie” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Tokeny odświeżania i wygasanie”?
Rotuj tokeny dostępu bez ponownego logowania. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Flask Academy?
Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Tokeny odświeżania i wygasanie”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?
Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Sesje a tokeny bezstanowe
- Wydawanie tokenów dostępu podczas logowania
- Ochrona endpointów za pomocą jwt_required
- Tokeny odświeżania i wygasanie