Authentification par jeton et JWT
Authentifiez les clients de l’API en toute sécurité.
Authentification par jeton et JWT est une leçon Django Academy gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Django Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Django Academy comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
APIs Need Stateless Auth
Browsers use sessions, but API clients often have no cookies. Token authentication lets a client prove who it is on every request instead.
How a Token Works
The user logs in once and gets a token string. They send it with each request, and DRF maps it back to the right user.
Enabling TokenAuthentication
Add the authtoken app and turn on TokenAuthentication so DRF knows to read tokens from incoming requests.
INSTALLED_APPS += ['rest_framework.authtoken']
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}Issuing a Token
DRF gives you a built-in obtain_auth_token view. Post a username and password to it, and it returns that user's token.
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path('api-token-auth/', obtain_auth_token),
]Sending the Token
The client puts the token in the Authorization header, prefixed with the word Token, on every protected call. 🔑
Authorization: Token 9944b09199c62bcf9418ad846dd0e4The Limit of Simple Tokens
A DRF token never expires and is just a database lookup. For larger systems, a JWT adds expiry and self-contained data.
What a JWT Carries
A JSON Web Token is a signed string holding claims like the user id and an expiry time. The server trusts it without a DB hit.
Adding SimpleJWT
Install djangorestframework-simplejwt and register its authentication class to swap tokens for JWTs.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}Access and Refresh Tokens
SimpleJWT gives two tokens: a short-lived access token for requests and a longer refresh token to get a new access token.
JWT Login Endpoints
Wire up TokenObtainPairView to log in and TokenRefreshView to renew, and your JWT flow is ready.
from rest_framework_simplejwt.views import (
TokenObtainPairView, TokenRefreshView)
urlpatterns = [
path('token/', TokenObtainPairView.as_view()),
path('token/refresh/', TokenRefreshView.as_view()),
]Sending a Bearer Token
JWT clients use the Bearer scheme in the Authorization header instead of the Token word DRF tokens use. 🎟️
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9Quick Check
One key difference separates DRF tokens from JWTs.
Recap: Proving Identity
You learned to authenticate API clients with DRF tokens for simplicity and JWTs for expiry and scale, both sent in the Authorization header. 🎉
Questions Fréquemment Posées
La leçon « Authentification par jeton et JWT » est-elle gratuite ?
Oui — le texte complet de « Authentification par jeton et JWT » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Django Academy, passe à CoddyKit PRO. Le cours Django Academy comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Authentification par jeton et JWT » ?
Authentifiez les clients de l’API en toute sécurité. Tu pratiques Django Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Django Academy ?
Aucune expérience préalable n'est requise. Django Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Authentification par jeton et JWT » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Django Academy ?
Oui. Chaque leçon Django Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- ModelViewSet et routeurs
- Autorisations et limitation de débit
- Authentification par jeton et JWT
- Filtrage, recherche et pagination