Sử dụng djangorestframework-simplejwt để cấp access/refresh token JWT cho API.
Cấu hình
pip install djangorestframework-simplejwt djangorestframeworkSettings:
INSTALLED_APPS += ['rest_framework_simplejwt', 'rest_framework'] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ) }URLs:
path('api/token/', TokenObtainPairView.as_view()), path('api/token/refresh/', TokenRefreshView.as_view())
Sử dụng
POST
/api/token/{username, password} → nhậnaccess,refreshtoken.Header
Authorization: Bearer <access>cho các request cần auth.POST
/api/token/refresh/{refresh} → access token mới.