Ochrona endpointów za pomocą jwt_required
Weryfikuj token przy każdym wywołaniu API.
Ochrona endpointów za pomocą jwt_required to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 3 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.
Guarding a Route
A token is only useful if some routes demand it. The jwt_required decorator turns any view into a members-only endpoint. 🔒
Apply the Decorator
Stack @jwt_required() under your route. Now the view only runs when a valid, unexpired token arrives.
@app.get("/profile")
@jwt_required()
def profile():
...Where the Token Goes
Clients send the token in the Authorization header using the Bearer scheme. Flask-JWT-Extended reads it from there automatically.
Authorization: Bearer <your-token>Automatic Rejection
No token, or a bad one? The decorator stops the request before your code runs and returns a 401. You write zero checking logic.
Who Is Calling
Inside a guarded view, call get_jwt_identity to read the identity you signed at login, usually the user id.
from flask_jwt_extended import get_jwt_identity
uid = get_jwt_identity()Read the Full Claims
Need extra claims like a role? get_jwt returns the whole payload as a dict so you can branch on what is inside.
from flask_jwt_extended import get_jwt
role = get_jwt().get("role")Use the Identity
With the user id in hand, load that user and serve their data. The token told you who, so the rest is normal Flask. 🙂
user = User.query.get(get_jwt_identity())Optional Protection
Want a page that adapts whether or not someone is logged in? Use optional=True so the view runs either way.
@jwt_required(optional=True)Customize the Error
Register an unauthorized_loader to return a friendly JSON message instead of the default when a token is missing.
@jwt.unauthorized_loader
def missing(reason):
return {"msg": reason}, 401Expired Tokens
An expired token is also rejected by the decorator. The client must log in again or refresh to keep going, which keeps stolen tokens short-lived.
Never Trust the Body
Read the caller from the token, never from a user id in the request body. The signature is what makes the identity trustworthy.
Quick Check
Recall how a guarded view learns who is calling.
Recap
Add @jwt_required() to guard a route, send the token as a Bearer header, and read the caller with get_jwt_identity. 🔐
Często zadawane pytania
Czy lekcja „Ochrona endpointów za pomocą jwt_required” jest bezpłatna?
Tak — pełny tekst „Ochrona endpointów za pomocą jwt_required” 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 „Ochrona endpointów za pomocą jwt_required”?
Weryfikuj token przy każdym wywołaniu API. Ć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 3 z 4.
Ile czasu zajmuje lekcja „Ochrona endpointów za pomocą jwt_required”?
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