pip install djangorestframework_simplejwt
settings.py
设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
# 默认鉴权用户,可更改
AUTH_USER_MODEL = "auth_user"
在用户的模型类里面,设置这个属性:USERNAME_FIELD = ‘username’,可以用来声明哪一个是 username 字段
路由配置:
from django.urls import path
from rest_framework_simplejwt import views as JWTAuthenticationViews
urlpatterns = [
path('api/token/', JWTAuthenticationViews.TokenObtainPairView.as_view(), name='get_token'),
path('api/token/refresh/', JWTAuthenticationViews.TokenRefreshView.as_view(), name='refresh_token'),
]
视图参考:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
class HelloView(APIView):
permission_classes = (IsAuthenticated,) # 重点
def get(self, request):
print('authenticators:', request.authenticators)
print('successful_authenticator:', request.successful_authenticator)
print('authenticate: ', request.successful_authenticator.authenticate(request))
print('authenticate_header: ', request.successful_authenticator.authenticate_header(request))
print('get_header: ', request.successful_authenticator.get_header(request))
print('get_raw_token: ', request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request)))
print('get_validated_token: ', request.successful_authenticator.get_validated_token(request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request))))
print('get_user: ', request.successful_authenticator.get_user(request.successful_authenticator.get_validated_token(request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request)))))
print('www_authenticate_realm: ', request.successful_authenticator.www_authenticate_realm)
return Response("OK")
api/token/
,携带 username
, password
, 例:( k, 123)返回示例:
{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTQ1MjI0MjU5LCJqdGkiOiIyYmQ1NjI3MmIzYjI0YjNmOGI1MjJlNThjMzdjMTdlMSIsInVzZXJfaWQiOjF9.D92tTuVi_YcNkJtiLGHtcn6tBcxLCBxz9FKD3qzhUg8",
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU0NTMxMDM1OSwianRpIjoiMjk2ZDc1ZDA3Nzc2NDE0ZjkxYjhiOTY4MzI4NGRmOTUiLCJ1c2VyX2lkIjoxfQ.rA-mnGRg71NEW_ga0sJoaMODS5ABjE5HnxJDb0F8xAo"
}
access
是 token
主体,有效期 5 分钟, refresh
是刷新 token
时需要的参数,两个都需要保存
api/token/refresh/
,携带 refresh
刷新 token
时,不需要 username
和 password
返回示例:
{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTQ1MjI0MjU5LCJqdGkiOiIyYmQ1NjI3MmIzYjI0YjNmOGI1MjJlNThjMzdjMTdlMSIsInVzZXJfaWQiOjF9.D92tTuVi_YcNkJtiLGHtcn6tBcxLCBxz9FKD3qzhUg8"
}
只有 access
,没有 refresh
设置请求的 Authorization
,类型为 Bearer Token
,值为 access
的值( token 主体)
下图为 Postman 请求示例:
authenticate: (, None)
authenticate_header: Bearer realm="api"
get_header: b'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTY1MTcxMjMxLCJqdGkiOiIyMjFmZDg0YjdlNTg0N2VmOWE5ZmZhM2FiNjAwYjUzOSIsInVzZXJfaWQiOjF9.crSbU4BDgTYSKPO6RxQJ8FVvx8XxVF9v8U3G-puDGEE'
get_raw_token: b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTY1MTcxMjMxLCJqdGkiOiIyMjFmZDg0YjdlNTg0N2VmOWE5ZmZhM2FiNjAwYjUzOSIsInVzZXJfaWQiOjF9.crSbU4BDgTYSKPO6RxQJ8FVvx8XxVF9v8U3G-puDGEE'
get_validated_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTY1MTcxMjMxLCJqdGkiOiIyMjFmZDg0YjdlNTg0N2VmOWE5ZmZhM2FiNjAwYjUzOSIsInVzZXJfaWQiOjF9.crSbU4BDgTYSKPO6RxQJ8FVvx8XxVF9v8U3G-puDGEE
get_user: k
www_authenticate_realm: api