在项目开发中不少开发者使用cookiecutter来构建Django项目的初始化模版,这样节省了大量的时间和精力,能更快速的开发。可是cookiecutter中设定的用户注册认证登录模块django-allauth封装了整个模块,对先后端不分离项目更友好,可是若是先后端项目分离,不少的API没法使用,对开发形成很大的问题,为了解决这一问题,django-rest-auth应运而生,开放出部分API用于用户的管理python
特色:django
结构:json
url(r'^rest-auth/', include('rest_auth.urls'))
这里须要特别注意:若是终端使用的是zsh,必须使用引号把django-rest-auth[with_social]括起来,若是不括起来会报错:zsh: no matches found: django-rest-auth[with_social]
url(r'^rest-auth/', include('rest_auth.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
注意:路由中的rest_auth名字不是固定的,能够进行修改
parameter:后端
### Registration POST http://127.0.0.1:8000/auth/registration/ HTTP/2.0 Content-Type: application/json { "username": "liquhua008", "password1": "liqh930215", "password2": "liqh930215", "email": "695762725@234523.com" }
HTTP/1.1 415 Unsupported Media Type Date: Thu, 03 Dec 2020 02:23:15 GMT Server: WSGIServer/0.2 CPython/3.7.0 Content-Type: application/json Vary: Accept Allow: POST, OPTIONS X-Frame-Options: DENY Content-Length: 62 X-Content-Type-Options: nosniff Referrer-Policy: same-origin { "detail": "Unsupported media type \"text/plain\" in request." }
报链接拒绝的错误或者CSRF错误cookie
解决:设置权限session
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', ] }
{ "key": "06e7a7767b5da07257297941c29621ac842b0c9e" }
parameter:app
HTTP/1.1 200 OK Date: Thu, 03 Dec 2020 02:41:39 GMT Server: WSGIServer/0.2 CPython/3.7.0 Content-Type: application/json Vary: Accept, Cookie Allow: POST, OPTIONS X-Frame-Options: DENY Content-Length: 50 X-Content-Type-Options: nosniff Referrer-Policy: same-origin Set-Cookie: csrftoken=vppzMvcQcFpab9kFeNenX3cUVvOzaK59Cfa0JNQIpqkNxw7yiQK8XXJnrQ4YI1cd; expires=Thu, 02 Dec 2021 02:41:39 GMT; Max-Age=31449600; Path=/; SameSite=Lax,sessionid=7ngs826bws34mdjkbb6f60xsuikzjmi1; expires=Thu, 17 Dec 2020 02:41:39 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax { "key": "1abc5ac07aab3395dfe4e832f7507250af4783a9" }
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated class UserDetailView(APIView): permission_classes = [IsAuthenticated, ] def get(self, request, *args, **kwargs): return Response({"email": request.user.email}, status=200) user_detail_view = UserDetailView.as_view()
from django.contrib import admin from django.urls import path, include, re_path from .views import ( user_detail_view ) urlpatterns = [ path('admin/', admin.site.urls), re_path(r'^auth/', include('rest_auth.urls')), re_path(r'^auth/registration/', include('rest_auth.registration.urls')), path('me/', user_detail_view) # 获取登录用户的邮箱 ]
### Me GET http://127.0.0.1:8000/me/ HTTP/2.0 Content-Type: application/json Authorization: Token 1abc5ac07aab3395dfe4e832f7507250af4783a9
HTTP/1.1 401 Unauthorized Date: Thu, 03 Dec 2020 02:50:18 GMT Server: WSGIServer/0.2 CPython/3.7.0 Content-Type: application/json WWW-Authenticate: Token Vary: Accept Allow: GET, HEAD, OPTIONS X-Frame-Options: DENY Content-Length: 58 X-Content-Type-Options: nosniff Referrer-Policy: same-origin { "detail": "Authentication credentials were not provided." }
相关介绍视频:JustDjango的dajngo-rest-authide