zoukankan      html  css  js  c++  java
  • rest_framework 认证功能

    from django.views import View
    from rest_framework.views import APIView
    from rest_framework.authentication import BasicAuthentication
    from rest_framework import exceptions
    from rest_framework.request import Request
    
    class MyAuthentication(object):
        def authenticate(self,request):
            token = request._request.GET.get('token')
            # 获取用户名和密码,去数据校验
            if not token:
                raise exceptions.AuthenticationFailed('用户认证失败')
            return ("alex",None)
    
        def authenticate_header(self,val):
            pass
    
    class DogView(APIView):
        authentication_classes = [MyAuthentication,]
    
        def get(self,request,*args,**kwargs):
            print(request)
            print(request.user)
            self.dispatch
            ret  = {
                'code':1000,
                'msg':'xxx'
            }
            return HttpResponse(json.dumps(ret),status=201)
    
        def post(self,request,*args,**kwargs):
            return HttpResponse('创建Dog')
    
        def put(self,request,*args,**kwargs):
            return HttpResponse('更新Dog')
    
        def delete(self,request,*args,**kwargs):
            return HttpResponse('删除Dog')

    1. 认证
    a. 问题1:有些API需要用户登录成功之后,才能访问;有些无需登录就能访问。
    b. 基本使用认证组件
    解决:
    a. 创建两张表
    b. 用户登录(返回token并保存到数据库)
    c. 认证流程原理
    - 见图示

    d. 再看一遍源码
    1. 局部视图使用&全局使用
    2. 匿名是request.user = None

    e. 内置认证类
    1. 认证类,必须继承:from rest_framework.authentication import BaseAuthentication
    2. 其他认证类:BasicAuthentication

    梳理:
    1. 使用
    - 创建类:继承BaseAuthentication; 实现:authenticate方法
    - 返回值:
    - None,我不管了,下一认证来执行。
    - raise exceptions.AuthenticationFailed('用户认证失败') # from rest_framework import exceptions
    - (元素1,元素2) # 元素1赋值给request.user; 元素2赋值给request.auth

    - 局部使用
    from rest_framework.authentication import BaseAuthentication,BasicAuthentication
    class UserInfoView(APIView):
    """
    订单相关业务
    """
    authentication_classes = [BasicAuthentication,]
    def get(self,request,*args,**kwargs):
    print(request.user)
    return HttpResponse('用户信息')
    - 全局使用:
    REST_FRAMEWORK = {
    # 全局使用的认证类
    "DEFAULT_AUTHENTICATION_CLASSES":['api.utils.auth.FirstAuthtication','api.utils.auth.Authtication', ],
    # "UNAUTHENTICATED_USER":lambda :"匿名用户"
    "UNAUTHENTICATED_USER":None, # 匿名,request.user = None
    "UNAUTHENTICATED_TOKEN":None,# 匿名,request.auth = None
    }
    2. 源码流程
    - dispatch
    - 封装request
    - 获取定义的认证类(全局/局部),通过列表生成时创建对象。
    - initial
    - perform_authentication
    request.user(内部循环....)

  • 相关阅读:
    初级安全入门——安全漏洞的检测与利用
    Gazebo11的安装与启动
    ROS入门(四)——Gazebo的基本使用
    ROS入门(二)——服务和全局参数
    ROS入门(一)——基本概念和话题
    数据结构与算法(三+)——列表的Java实现
    Java EE入门(二十)——Maven基础
    Java EE入门(十九)——Redis基础
    Java EE入门(十八)——Ajax&JSON基础
    npm中如何更新自己已经发布的1.0.0的模块包?
  • 原文地址:https://www.cnblogs.com/Liang-jc/p/9314056.html
Copyright © 2011-2022 走看看