zoukankan      html  css  js  c++  java
  • drf认证权限频率

    认证:

    创建认证类(继承BaseAuthentication),重写authenticate,在该方法中校验,如果校验通过返回None,如果校验失败,抛出异常,校验通过也可以返回当前用户和auth,这样在视图类中可以通过request.user,获取当前登录用户,一旦返回值,后续所有认证类都将不再执行,

    视图类中:

    authentication_classes=[MyAuthentication]

    源码执行流程:

    authentication_classes查找顺序(认证类的查找顺序):先在自己中找:authentication_classes=[MyAuthentication]如果没有配置:取配置文件settings中找,如果项目的settings中没有配置,则去drf中内置的settings中找

    局部使用:

    在视图类中配置:authentication_classes=[MyAythentication]

    全局使用:

    在settings中配置:

    REST_FRAMEWORK={'DEFAULT_AUTHENTICATION_CLASSES':['app.MyAuth.Myauthentication',]}

    全局使用局部禁用:

    在视图类中配置:authentication=[]

    权限:(不同用户类型)

    权限类:

    from rest_framework.permissions import BasePermission

    class MyPermission(BasePermission):

      def has_permission(self,request,view):

        if request.user.type==1:

          user_str=request.user.type.get_type_display()

          return True

        else:

          return False

    局部使用:

    在视图类中配置:

    permission_classes=[MyPermissions]

    全局使用:

    在settings中配置:

    'DEFAULT_PERMISSION_CLASSES':['app.MyAuth.MyPermission',],}

    源码分析:

    APIView中的:check_permissions(self,request),进行的权限校验-配置错误信息的显示

    message="错误提示"

    频率的使用:

    创建类

    from rest_framework.throttling import SimpleRateThrottle

    class MyThrottling(SimpleRateThrottle):

      scope="xxx"

      #必须重写get_cache_key,返回什么,频率组件就会以什么做出限制

      def get_cache_key(self,request,view):

        return request.META.get('REMOTE_ADDR')

    在settings中配置:

    'DEFAULT_THROTTLE_RATES':{'xxx':'10/day'}

    局部使用:

    在视图类中:throttle_classes=[MyThrottling]

    全局使用:

    在settings中配置:

    'DEFAULT_THROTTLE_RATES':['app.MyAuth.MyThrottling'],

    源码分析:

    补充:

    token信息放到请求头中,如何去?

    token=request.META.get('token')

    2.django的GET,POST,META,body分别对应HTTP请求的哪部分?

    GET:http请求路径中数据部分

    POST:http请求以urlencode/formdata形式编码的body体部分

    META:http请求头部信息

    body:http请求请求体部分

    path,get_full_path(),FILES

  • 相关阅读:
    python修改pip源
    python if条件判断dataframe是否为空
    bowtie和bowtie2使用条件区别及用法
    bat批处理for循环嵌套
    常用的数据标准化方法
    python学习——利用循环实现分类散点图绘制
    python学习——通过命令行参数根据fasta文件中染色体id提取染色体序列
    python学习——把读取fasta文件的代码封装成函数
    python学习——把计算GC含量的代码封装成函数
    python学习——使用argparse参数解释器传递命令行参数
  • 原文地址:https://www.cnblogs.com/suncunxu/p/10615268.html
Copyright © 2011-2022 走看看