一:使用RestFramwork,定义一个视图

  1. from rest_framework.viewsets import ModelViewSet
  2.  
  3. class BookView(ModelViewSet):
  4. queryset = Book.objects.all()
  5. serializer_class = BookSerializer

认证、频率和权限组件都由继承的ModelViewSet支持,所以要了解这三个组件的具体如何实现

对认证、频率、权限的管理就需要进入到其中查看

二:首先来了解组件认证

由上图可以看到ModelViewSet继承了六个类,前面的五个并没有组件的内容,不去管,接下来进入GenericViewSet类中看看

GenericViewSet这个类没有具体的代码,但是可以看到它继承了两个类ViewSetMixin,和generics.GenericAPIView

ViewSetMixin

这个类中主要需要了解的是as_view这个在url中使用的方法,这个类只是重写了as_view中的view方法,具体的核心代码如下

  1. for method, action in actions.items():
  2. handler = getattr(self, action)
  3. setattr(self, method, handler)

简单来说,就是把url中传入的字典for循环,利用反射找到对应的方法重新设置get请求对应的函数

  1. url(r'^authors/$', views.AuthorModelView.as_view({"get":"list","post":"create"})),

如上:在Django启动后,views.AuthorModelView.as_view({"get":"list","post":"create"})的执行结果是一个闭包函数view

请求发送进来,根据闭包函数外的actions:{"get":"list","post":"create"}设置self.get = list或者设置 self.post= create等等

由上可知,这个函数也与要找的组件关系不大。

generics.GenericAPIView

  1. def get_queryset(self):
  2. def get_object(self):
  3. def get_serializer(self, *args, **kwargs):
  4. def get_serializer_class(self):
  5. def get_serializer_context(self):
  6. def filter_queryset(self, queryset):
  7. @property
  8. def paginator(self):
  9. def paginate_queryset(self, queryset):
  10. def get_paginated_response(self, data):

类中方法与要找组件无关,继续进入其父类中找

在父类APIView中的dispach方法中

self.initial(request, *args, **kwargs)这一段代码负责所有的认证、权限和频率管理

因为视图的继承复杂,现在需要搞清楚类的继承关系和代码具体运行步骤,才好往下走

继承关系图

请求执行流程

Django启动

url(r'^authors/$', views.AuthorModelView.as_view({"get":"list","post":"create"})) 在Django启动时就执行,as_view的执行结果是一个闭包函数

view,由actions = {"get":"list","post":"create"}等参数包裹:

实际路由为:url(r'^authors/$', view)

请求到来:

根据继承关系:请求到来执行的view函数是类ViewSetMixin中的闭包函数view

view源代码

  1. def view(request, *args, **kwargs):
  2. self = cls(**initkwargs)
  3. # We also store the mapping of request methods to actions,
  4. # so that we can later set the action attribute.
  5. # eg. `self.action = 'list'` on an incoming GET request.
  6. self.action_map = actions
  7.  
  8. # Bind methods to actions
  9. # This is the bit that's different to a standard view
  10. for method, action in actions.items():
  11. handler = getattr(self, action)
  12. setattr(self, method, handler)
  13.  
  14. if hasattr(self, 'get') and not hasattr(self, 'head'):
  15. self.head = self.get
  16.  
  17. self.request = request
  18. self.args = args
  19. self.kwargs = kwargs
  20.  
  21. # And continue as usual
  22. return self.dispatch(request, *args, **kwargs)

可以看到,在将self.get,self.post等方法映射之后,view方法最终返回了self.dispatch(request, *args, **kwargs)的执行结果

根据对象的属性和方法查找原则,self.dispatchfan方法调用的是类APIView中的dispatch方法

dispatch源码

  1. def dispatch(self, request, *args, **kwargs):
  2. """
  3. `.dispatch()` is pretty much the same as Django's regular dispatch,
  4. but with extra hooks for startup, finalize, and exception handling.
  5. """
  6. self.args = args
  7. self.kwargs = kwargs
  8. request = self.initialize_request(request, *args, **kwargs)
  9. self.request = request
  10. self.headers = self.default_response_headers # deprecate?
  11.  
  12. try:
  13. self.initial(request, *args, **kwargs) # 认证、频率、权限相关
  14.  
  15. # Get the appropriate handler method
  16. if request.method.lower() in self.http_method_names:
  17. handler = getattr(self, request.method.lower(),
  18. self.http_method_not_allowed)
  19. else:
  20. handler = self.http_method_not_allowed
  21.  
  22. response = handler(request, *args, **kwargs)
  23.  
  24. except Exception as exc:
  25. response = self.handle_exception(exc)
  26.  
  27. self.response = self.finalize_response(request, response, *args, **kwargs)
  28. return self.response

dispatch的核心功能就是根据请求的方法不同,分发执行不同的代码,并最终返回结果。

在这里我注意到,每次请求分发之前都会执行self.initial(request, *args, **kwargs) 这段代码,也就是说每次请求都会进入这里执行。

initial源代码

  1. def initial(self, request, *args, **kwargs):
  2. """
  3. Runs anything that needs to occur prior to calling the method handler.
  4. """
  5. self.format_kwarg = self.get_format_suffix(**kwargs)
  6.  
  7. # Perform content negotiation and store the accepted info on the request
  8. neg = self.perform_content_negotiation(request)
  9. request.accepted_renderer, request.accepted_media_type = neg
  10.  
  11. # Determine the API version, if versioning is in use.
  12. version, scheme = self.determine_version(request, *args, **kwargs)
  13. request.version, request.versioning_scheme = version, scheme
  14.  
  15. # Ensure that the incoming request is permitted
  16. self.perform_authentication(request) # 认证
  17. self.check_permissions(request) # 权限
  18. self.check_throttles(request) # 频率

initial中的核心代码是依次执行:

self.perform_authentication(request) # 认证

self.check_permissions(request) # 权限

self.check_throttles(request) # 频率

也就是:认证之后才会验证权限,权限验证之后才会验证频率

perform_authentication源代码

  1. def perform_authentication(self, request):
  2. request.user

perform_authentication中返回了request.user,首先要明白这个request来自于哪里?

从dispatch中一路过来,request一直没做处理,说明request至少来自于dispatch,APIView中dispatch的源码中有一行代码可以解释request的来源

  1. request = self.initialize_request(request, *args, **kwargs)
  2. self.request = request

initialize_request源码

  1. def initialize_request(self, request, *args, **kwargs):
  2. """
  3. Returns the initial request object.
  4. """
  5. parser_context = self.get_parser_context(request)
  6.  
  7. return Request(
  8. request,
  9. parsers=self.get_parsers(),
  10. authenticators=self.get_authenticators(),
  11. negotiator=self.get_content_negotiator(),
  12. parser_context=parser_context
  13. )

initialize_request代码中返回了一个Request类的对象,传入了

request
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context

  1. def __init__(self, request, parsers=None, authenticators=None,
  2. negotiator=None, parser_context=None):
  3. assert isinstance(request, HttpRequest), (
  4. 'The `request` argument must be an instance of '
  5. '`django.http.HttpRequest`, not `{}.{}`.'
  6. .format(request.__class__.__module__, request.__class__.__name__)
  7. )
  8.  
  9. self._request = request
  10.  
  11. @property
  12. def user(self):
  13. """
  14. Returns the user associated with the current request, as authenticated
  15. by the authentication classes provided to the request.
  16. """
  17. if not hasattr(self, '_user'):
  18. with wrap_attributeerrors():
  19. self._authenticate()
  20. return self._user

perform_authentication代码中执行的request.user就是执行的Request类的user方法

user方法中的代码代码表示如果没有_user属性就执行self._authenticate()

 _authenticate源代码

  1. def _authenticate(self):
  2. """
  3. Attempt to authenticate the request using each authentication instance
  4. in turn.
  5. """
  6. for authenticator in self.authenticators:
  7. try:
  8. user_auth_tuple = authenticator.authenticate(self)
  9. except exceptions.APIException:
  10. self._not_authenticated()
  11. raise
  12.  
  13. if user_auth_tuple is not None:
  14. self._authenticator = authenticator
  15. self.user, self.auth = user_auth_tuple
  16. return
  17.  
  18. self._not_authenticated()

 _authenticate:for循环self.authenticators并赋值给authenticator,然后执行authenticate方法

首先要知道self.authenticators来自于哪里?

回溯代码:

_authenticate中调用了self.authenticators。

self对象来自于user方法

user方法中的self对象Request的实例化对象

Request的实例化对象的实例化对象有一个属性:

self.authenticators= authenticators or ()

authenticators 是一个Request类的实例化参数,默认为None,如果有传入参数则为传入的值

initialize_request源代码中实例化时:authenticators=self.get_authenticators(),

  1. return Request(
  2. request,
  3. parsers=self.get_parsers(),
  4. authenticators=self.get_authenticators(),
  5. negotiator=self.get_content_negotiator(),
  6. parser_context=parser_context
  7. )

这时的self来自于调用initialize_request的对象

initialize_request在dispatch中被调用,dispatch的调用对象即是自定义的视图类的实例化对象

也即使说self.get_authenticators()是视图类调用的get_authenticators方法

get_authenticators源代码

  1. def get_authenticators(self):
  2.  
  3. return [auth() for auth in self.authentication_classes]

get_authenticators中for循环视图类的authentication_classes的属性,加括号实例化组成一个列表返回

于是查找对象的属性,首先从对象自己找,然后从视图类中找,如果找不到,在依照继承关系从被继承的类中找

在被视图类所继承的类APIView中找到authentication_classes属性的定义

  1. class APIView(View):
  2.  
  3. # The following policies may be set at either globally, or per-view.
  4. renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
  5. parser_classes = api_settings.DEFAULT_PARSER_CLASSES
  6. authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
  7. throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES
  8. permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
  9. content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS
  10. metadata_class = api_settings.DEFAULT_METADATA_CLASS
  11. versioning_class = api_settings.DEFAULT_VERSIONING_CLASS
  12.  
  13. # Allow dependency injection of other settings to make testing easier.
  14. settings = api_settings
  1. api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
  1. APISettings类中并没有DEFAULT_AUTHENTICATION_CLASSES属性,自动触发__getattr__方法

APISettings源码

  1. class APISettings(object):
  2.  
  3. def __init__(self, user_settings=None, defaults=None, import_strings=None):
  4. if user_settings: # 如果user_settings有值执行下列代码
  5. self._user_settings = self.__check_user_settings(user_settings)
  6. self.defaults = defaults or DEFAULTS
  7. # defaults有值则赋给self.defaults,没有则把DEFAULTS赋值给self.defaults
  8. self.import_strings = import_strings or IMPORT_STRINGS
  9. self._cached_attrs = set()
  10.  
  11. @property
  12. def user_settings(self):
  13. if not hasattr(self, '_user_settings'): # 如果_user_settings没有定义
  14. self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
  15. # 从Django项目的settings文件中利用反射取出'REST_FRAMEWORK'的值赋给self._user_settings
  16. return self._user_settings
  17.  
  18. def __getattr__(self, attr): # 对象用.attr的方法查找不到属性时自动触发
  19. if attr not in self.defaults: # 如果self.defaults中没有查找的属性则报错
  20. raise AttributeError("Invalid API setting: '%s'" % attr)
  21.  
  22. try:
  23. # Check if present in user settings
  24. val = self.user_settings[attr]
  25. # 从self.user_settings执行返回的值中取出属性attr的的值赋给val
  26. except KeyError:
  27. # Fall back to defaults
  28. val = self.defaults[attr]
  29.  
  30. # Coerce import strings into classes
  31. if attr in self.import_strings:
  32. # 如果属性attr在self.import_strings中通过反射取出对应的相应的方法或属性做进一步处理
  33. val = perform_import(val, attr)
  34.  
  35. # Cache the result
  36. self._cached_attrs.add(attr)
  37. setattr(self, attr, val) # 利用反射给视图类对象设置一个属性attr值为val
  38. return val
  1. DEFAULTS = {
  2. # Base API policies
  3. 'DEFAULT_RENDERER_CLASSES': (
  4. 'rest_framework.renderers.JSONRenderer',
  5. 'rest_framework.renderers.BrowsableAPIRenderer',
  6. ),
  7. 'DEFAULT_PARSER_CLASSES': (
  8. 'rest_framework.parsers.JSONParser',
  9. 'rest_framework.parsers.FormParser',
  10. 'rest_framework.parsers.MultiPartParser'
  11. ),
  12. 'DEFAULT_AUTHENTICATION_CLASSES': (
  13. 'rest_framework.authentication.SessionAuthentication',
  14. 'rest_framework.authentication.BasicAuthentication'
  15. ),
  16. 'DEFAULT_PERMISSION_CLASSES': (
  17. 'rest_framework.permissions.AllowAny',
  18. ),
  19. 'DEFAULT_THROTTLE_CLASSES': (),
  20. 'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'rest_framework.negotiation.DefaultContentNegotiation',
  21. 'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata',
  22. 'DEFAULT_VERSIONING_CLASS': None,
  23.  
  24. # Generic view behavior
  25. 'DEFAULT_PAGINATION_CLASS': None,
  26. 'DEFAULT_FILTER_BACKENDS': (),
  27.  
  28. # Schema
  29. 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
  30.  
  31. # Throttling
  32. 'DEFAULT_THROTTLE_RATES': {
  33. 'user': None,
  34. 'anon': None,
  35. },
  36. 'NUM_PROXIES': None,
  37.  
  38. # Pagination
  39. 'PAGE_SIZE': None,
  40.  
  41. # Filtering
  42. 'SEARCH_PARAM': 'search',
  43. 'ORDERING_PARAM': 'ordering',
  44.  
  45. # Versioning
  46. 'DEFAULT_VERSION': None,
  47. 'ALLOWED_VERSIONS': None,
  48. 'VERSION_PARAM': 'version',
  49.  
  50. # Authentication
  51. 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
  52. 'UNAUTHENTICATED_TOKEN': None,
  53.  
  54. # View configuration
  55. 'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
  56. 'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
  57.  
  58. # Exception handling
  59. 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
  60. 'NON_FIELD_ERRORS_KEY': 'non_field_errors',
  61.  
  62. # Testing
  63. 'TEST_REQUEST_RENDERER_CLASSES': (
  64. 'rest_framework.renderers.MultiPartRenderer',
  65. 'rest_framework.renderers.JSONRenderer'
  66. ),
  67. 'TEST_REQUEST_DEFAULT_FORMAT': 'multipart',
  68.  
  69. # Hyperlink settings
  70. 'URL_FORMAT_OVERRIDE': 'format',
  71. 'FORMAT_SUFFIX_KWARG': 'format',
  72. 'URL_FIELD_NAME': 'url',
  73.  
  74. # Input and output formats
  75. 'DATE_FORMAT': ISO_8601,
  76. 'DATE_INPUT_FORMATS': (ISO_8601,),
  77.  
  78. 'DATETIME_FORMAT': ISO_8601,
  79. 'DATETIME_INPUT_FORMATS': (ISO_8601,),
  80.  
  81. 'TIME_FORMAT': ISO_8601,
  82. 'TIME_INPUT_FORMATS': (ISO_8601,),
  83.  
  84. # Encoding
  85. 'UNICODE_JSON': True,
  86. 'COMPACT_JSON': True,
  87. 'STRICT_JSON': True,
  88. 'COERCE_DECIMAL_TO_STRING': True,
  89. 'UPLOADED_FILES_USE_URL': True,
  90.  
  91. # Browseable API
  92. 'HTML_SELECT_CUTOFF': 1000,
  93. 'HTML_SELECT_CUTOFF_TEXT': "More than {count} items...",
  94.  
  95. # Schemas
  96. 'SCHEMA_COERCE_PATH_PK': True,
  97. 'SCHEMA_COERCE_METHOD_NAMES': {
  98. 'retrieve': 'read',
  99. 'destroy': 'delete'
  100. },
  101. }

DEFAULTS

在本例中视图类中并没有重写authentication_classes,因此根据APISettings中的代码可知,程序首先在Django的settings文件中查找,由于settins文件中没有定义,因此抛出异常,最终从DEFAULTS中取得了authentication_classes的值

最终APIView中authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES的执行结果是

  1. authentication_classes =
  2. (
  3. SessionAuthentication,
  4. BasicAuthentication
  5. ),

于是

  1. authenticators = [SessionAuthentication(),BasicAuthentication()]

最终在 _authenticate源代码中执行的是SessionAuthentication,BasicAuthentication这两个方法中的authenticate(self, request)方法

  1. class SessionAuthentication(BaseAuthentication):
  2. """
  3. Use Django's session framework for authentication.
  4. """
  5.  
  6. def authenticate(self, request):
  7. """
  8. Returns a `User` if the request session currently has a logged in user.
  9. Otherwise returns `None`.
  10. """
  11.  
  12. # Get the session-based user from the underlying HttpRequest object
  13. user = getattr(request._request, 'user', None)
  14.  
  15. # Unauthenticated, CSRF validation not required
  16. if not user or not user.is_active:
  17. return None
  18.  
  19. self.enforce_csrf(request)
  20.  
  21. # CSRF passed with authenticated user
  22. return (user, None)
  23.  
  24. def enforce_csrf(self, request):
  25. """
  26. Enforce CSRF validation for session based authentication.
  27. """
  28. reason = CSRFCheck().process_view(request, None, (), {})
  29. if reason:
  30. # CSRF failed, bail with explicit error message
  31. raise exceptions.PermissionDenied('CSRF Failed: %s' % reason)
  1. authenticate方法的逻辑就是就是认证组件的实际逻辑
    根据整个源码的思路,可以在重新写一个认证类,而其中必定有authenticate方法来控制验证逻辑
  1. from rest_framework.exceptions import AuthenticationFailed
  2. from rest_framework.authentication import BaseAuthentication
  3.  
  4. class TokenAuth(BaseAuthentication):
  5.  
  6. def authenticate(self,request):
  7.  
  8. token=request.GET.get("token",None)
  9.  
  10. token_obj=UserToken.objects.filter(token=token).first()
  11. if token_obj:
  12. return token_obj.user.user,token_obj
  13. else:
  14. raise AuthenticationFailed("认证失败!")
  1.  

Rest-Framework组件源码之认证、频率、权限的更多相关文章

  1. Django框架之DRF 认证组件源码分析、权限组件源码分析、频率组件源码分析

    认证组件 权限组件 频率组件

  2. Rest_Framework之认证、权限、频率组件源码剖析

    一:使用RestFramwork,定义一个视图 from rest_framework.viewsets import ModelViewSet class BookView(ModelViewSet ...

  3. Django-restframework 源码之认证组件源码分析

    Django-restframework 源码之认证组件源码分析 一 前言 之前在 Django-restframework 的流程分析博客中,把最重要的关于认证.权限和频率的方法找到了.该方法是 A ...

  4. Django REST framework —— 权限组件源码分析

    在上一篇文章中我们已经分析了认证组件源码,我们再来看看权限组件的源码,权限组件相对容易,因为只需要返回True 和False即可 代码 class ShoppingCarView(ViewSetMix ...

  5. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  6. 浅探element-ui2组件源码之upload

    最近不小心更新了element-ui的版本,已经到了2.1.0,以前修改的源码都失效了. 于是重新尝试下面的指令重新修改: git clone https://github.com/ElemeFE/e ...

  7. element-ui 组件源码分析整理笔记目录

    element-ui button组件 radio组件源码分析整理笔记(一) element-ui switch组件源码分析整理笔记(二) element-ui inputNumber.Card .B ...

  8. element-ui input组件源码分析整理笔记(六)

    input 输入框组件 源码: <template> <div :class="[ type === 'textarea' ? 'el-textarea' : 'el-in ...

  9. element-ui Message组件源码分析整理笔记(八)

    Message组件源码: main.js import Vue from 'vue'; import Main from './main.vue'; import { PopupManager } f ...

随机推荐

  1. Apache+Nginx+php共存(一)

    在实际开发中个人的电脑中经常需要安装 WNMRP.WAMRP.LNMRP.LAMRP等各种开发环境来应对不同的开发需求. 此篇主要是对WINDOWS系统下 Apache+Nginx + PHP +My ...

  2. python 安装pymssql

    error: command 'gcc' failed with exit status 1 ---------------------------------------- Command &quo ...

  3. vSphere下安装Hyper-V

    在vSphere 5.5中默认是无法嵌套安装Hyper-V的,必须在vSphere中稍作修改. 1. 勾选这两个选项,如果是灰色,请升级虚拟机版本至最新: 2. 在配置文件中手动加入这一行参数: 3. ...

  4. Android UI测量、布局、绘制过程探究

    在上一篇博客<Android中Activity启动过程探究>中,已经从ActivityThread.main()开始,一路摸索到ViewRootImpl.performTraversals ...

  5. 计算机网络【1】—— OSI七层协议和TCP/IP四层协议

    新开一贴,专门用来记录计算机网络相关知识. 一.OSI七层协议 物理层.数据链路层.网络层.传输层.会话层.表示层.应用层 二.TCP/IP四层协议 网络接口层.网际层.运输层.应用层 三.五层协议 ...

  6. JVM学习笔记(二):垃圾收集

    程序计数器. 虚拟机栈. 本地方法栈3个区域随线程而生,随线程而灭:栈中的栈帧随着方法的进入和退出而有条不紊地执行着出栈和入栈操作. 每一个栈帧中分配多少内存基本上是在类结构确定下来时就已知的,因此这 ...

  7. 【JavaScript】table里面点击某td获取同一行tr的其他td值

    某td的input(保存按钮)上绑定方法,点击按钮保存该行所有数据 function locationedit(num){ var ordernumber = $("#"+num) ...

  8. 题解 CF762A 【k-th divisor】

    emmm...不能说是水题吧...小金羊以为考的是STL(手动滑稽)... 行,这个题说让分解因数(不一定非得质因数). 我们考虑到有第k个数有可能有\(x\cdot x=n\)的毒瘤情况, 并且题目 ...

  9. 搜索引擎(Solr-索引详解)

    时间字段类型特别说明 Solr中提供的时间字段类型( DatePointField, DateRangeField,废除的TrieDateField )是以时间毫秒数来存储时间的. 要求字段值以ISO ...

  10. Docker Machine 和 Docker Engine 的区别

    Docker Engine 当人们提到 Docker,一般而言,大家说的是 Docker Engine,如下图:  它是一个 client-server application. Docker Eng ...