JWT(JSON Web Tokens) With Django REST Framework

代码: https://github.com/wktadmin/REST-Framework-Model-Relationships

教程: https://www.youtube.com/watch?v=Fhcn2qx-4VQ&t=223s

  1. setting里的基本权限设置:

REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), }
  1. TokenObtainPairView 和 TokenRefreshView
    one is an access tokens , one is refresh token

  2. urlpatterns

from django.conf.urls import url, include
from django.contrib import admin
# one is an access tokens , one is refresh token
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('languages.urls')),
    url(r'^api-atuh', include('rest_framework.urls')), 
    url(r'^api/token/', TokenObtainPairView.as_view()), # is only accept post request, get is not allowed
    url(r'^api/token/refresh/', TokenRefreshView.as_view()),
]


  • models serializer…详见代码