码迷,mamicode.com
首页 > 其他好文 > 详细

django-restfulframework认证源码解析

时间:2018-02-05 18:47:51      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:efault   raise   join   family   此刻   实例   ace   post   isp   

 

认证控制:检查用户是否登录,或携带某些元素.

当程序运行时,首先会调用程序的self.dispatch

技术分享图片
def dispatch(self, request, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        request = self.initialize_request(request, *args, **kwargs)   # 第一步
        self.request = request
        self.headers = self.default_response_headers  # 第二部
        try:
            self.initial(request, *args, **kwargs)     #第三步

            # Get the appropriate handler method
            if request.method.lower() in self.http_method_names:
                handler = getattr(self, request.method.lower(),
                                  self.http_method_not_allowed)
            else:
                handler = self.http_method_not_allowed

            response = handler(request, *args, **kwargs)

        except Exception as exc:
            response = self.handle_exception(exc)

        self.response = self.finalize_response(request, response, *args, **kwargs)
        return self.response
def dispatch()

 

 

第一步:调用initialize_request函数.封装了request,并且返回一个Request的对象,

技术分享图片
def initialize_request(self, request, *args, **kwargs):
        parser_context = self.get_parser_context(request)
        return Request(
            request,
            parsers=self.get_parsers(),  #[parser() for parser in self.parser_classes]对象的列表
            authenticators=self.get_authenticators(),  #同上都是返回对象列表
            negotiator=self.get_content_negotiator(),   #同上
            parser_context=parser_context
        )
def initialize_request()

第二部:调用 default_response_headers返回抱头,里面包含请求信息,形成的样式
headers={‘Allow‘:[‘get‘, ‘post‘, ‘put‘, ‘patch‘, ‘delete‘, ‘head‘, ‘options‘, ‘trace‘],}
@property
    def default_response_headers(self):
        headers = {
            Allow: , .join(self.allowed_methods),
        }
        if len(self.renderer_classes) > 1:  #如果配置有信息,会加上{Vary:"Accept"}
            headers[Vary] = Accept
        return headers

 


第三步: 调用initial,检查用户版本,用户认证,权限验证,以及访问频率的控制.
技术分享图片
def initial(self, request, *args, **kwargs):
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs) #版本控制
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
        self.perform_authentication(request) #用户认证
        self.check_permissions(request)     #权限验证
        self.check_throttles(request)       #访问频率控制
def initial()

 

 

 

分析用户认证:

 

1)   运行self.perform_authentication(request),返回request.user
技术分享图片
def perform_authentication(self, request):
    request.user    #此刻的request是 Request
def perform_authentication()
2)找到Request,并且运行user方法.
技术分享图片
@property
    def user(self):
        if not hasattr(self, _user):   #程序刚开始运行,没有用户登录,所以会执行这里
            with wrap_attributeerrors():
                self._authenticate()
        return self._user
def user()
3)  调用self._authenticate()
技术分享图片
def _authenticate(self):
        for authenticator in self.authenticators: #第4)步 实例化class ForcedAuthentication(object)
            try:
                user_auth_tuple = authenticator.authenticate(self)   #第5)步调用authenticate
            except exceptions.APIException:
                self._not_authenticated()
                raise

            if user_auth_tuple is not None:
                self._authenticator = authenticator         #第6步返回上面调用的东西
                self.user, self.auth = user_auth_tuple
                return                                      #第7步如果有东西传进来就return

        self._not_authenticated()
def _authenticate(self):

 

4)调用self.authenticators等于实例化 ForcedAuthentication类:
技术分享图片
class ForcedAuthentication(object):
    def __init__(self, force_user, force_token):
        self.force_user = force_user
        self.force_token = force_token
    def authenticate(self, request):
        return (self.force_user, self.force_token)
class ForcedAuthentication(object):
5)执行user_auth_tuple = authenticator.authenticate(self)也就是
ForcedAuthentication下面的authenticate方法:
def authenticate(self, request):
        return (self.force_user, self.force_token
返回里面的2个参数,参数可以自己定义,你传进去什么就是什么
6)返回self._authenticator = authenticator,这里能拿到数据必须是用user和auth才能拿
self.user, self.auth = user_auth_tuple

 

 

 

 

 

 

 

 

 

 

 

 

django-restfulframework认证源码解析

标签:efault   raise   join   family   此刻   实例   ace   post   isp   

原文地址:https://www.cnblogs.com/52forjie/p/8418480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!