Django的rest_framework认证组件之局部设置源码解析
前言:
Django的rest_framework组件的功能很强大,今天来我来给大家剖析一下认证组件
下面进入正文分析,我们从视图开始,一步一步来剖析认证组件
1、进入urls文件
url(r'^login/', views.LoginCBV.as_view(),name="login"),
2、然后执行LoginCBV这个类的as_view方法
3、LoginCBV这个类是我们自己的写的,但是LoginCBV类根本没有写as_view这个方法,那么我们该怎么办? 此时我们应该去找LoginCBV的父类,看父类是否as_view方法
4、先确认LoginCBV这个类的父类,很清楚,我们看到LoginCBV这个类的父类是APIView这个类
class LoginCBV(APIView):
5、下面我们去APIView这个类中查找是否有as_view这个方法,我们在APIView这个类中找到了as_view方法,这个被classmethod修饰符修饰,也就是说这个方法是一个类方法,由一个类本身就可以调用这个方法,这个时候大家在会议一下,在urls文件中,是不是一个类在调用as_view方法。
如果大家都classmethod这个修饰符不清楚,可以看下我的这篇博客:https://www.cnblogs.com/bainianminguo/p/10475204.html
@classmethod
def as_view(cls, **initkwargs):
6、下面我们来具体看下as_view这个方法,到底做了什么事情?下面是方法的源码
@classmethod
def as_view(cls, **initkwargs):
"""
Store the original class on the view function. This allows us to discover information about the view when we do URL
reverse lookups. Used for breadcrumb generation.
"""
if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
def force_evaluation():
raise RuntimeError(
'Do not evaluate the `.queryset` attribute directly, '
'as the result will be cached and reused between requests. '
'Use `.all()` or call `.get_queryset()` instead.'
)
cls.queryset._fetch_all = force_evaluation view = super(APIView, cls).as_view(**initkwargs)
view.cls = cls
view.initkwargs = initkwargs # Note: session based authentication is explicitly CSRF validated,
# all other authentication is CSRF exempt.
return csrf_exempt(view)
我们来重点看下需要我们知道的,首先这个函数的返回值是一个view方法
接着我们看下view这个方法,从这里我们可以看到,view就是执行APIView父类的as_view方法,下面我们接着去找APIView类的父类的as_view方法
7、进入APIView父类中,我们看到APIView类的父类是View
class APIView(View):
8、进入View类中,看下as_view这个方法到底了干了什么?
@classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs # take name and docstring from class
update_wrapper(view, cls, updated=()) # and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
下面我们来分析这个方法的源码,方法的返回值是view这个函数,而view这个函数的返回值是self.dispatch这个方法
9、下面我们首先要找到self.dispatch这个方法,然后在看下这个方法到底干了什么?
这个self到底是哪个类的实例呢?我们来梳理一下子类和父类的关系
LoginCBV【类】------->APIView【类】------->View【类】------>view【方法】-----》dispatch【方法】
那么我们就需要先从LoginCBV这个类中找dispatch方法,发现没有找到,然后继续找LoginCBV这个类的父类,也就是APIView这个类,看这个类是否dispatch方法
10、我们最终在APIView这个类中找到了dispatch方法,所以这里调的dispatch方法一定是APIView这个类的方法
def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate? 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
这个方法很重要,我们来看下
首先rest_framework处理后的request
然后看下self.initialize.request方法干了什么,当然找这个方法到底在是哪个类的方法,也是要按照之前我们找dispatch方法的一样,我这里就直接找到这个方法了,self.initialize.request这个方法是APIView这个类的方法
def initialize_request(self, request, *args, **kwargs):
"""
Returns the initial request object.
"""
parser_context = self.get_parser_context(request) return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
这个函数返回一个Request的实例对象,然后我们在看下Request这个类的,Request类的源码如下
class Request(object):
"""
Wrapper allowing to enhance a standard `HttpRequest` instance. Kwargs:
- request(HttpRequest). The original request instance.
- parsers_classes(list/tuple). The parsers to use for parsing the
request content.
- authentication_classes(list/tuple). The authentications used to try
authenticating the request's user.
""" def __init__(self, request, parsers=None, authenticators=None,
negotiator=None, parser_context=None):
assert isinstance(request, HttpRequest), (
'The `request` argument must be an instance of '
'`django.http.HttpRequest`, not `{}.{}`.'
.format(request.__class__.__module__, request.__class__.__name__)
) self._request = request
self.parsers = parsers or ()
self.authenticators = authenticators or ()
self.negotiator = negotiator or self._default_negotiator()
self.parser_context = parser_context
self._data = Empty
self._files = Empty
self._full_data = Empty
self._content_type = Empty
self._stream = Empty if self.parser_context is None:
self.parser_context = {}
self.parser_context['request'] = self
self.parser_context['encoding'] = request.encoding or settings.DEFAULT_CHARSET force_user = getattr(request, '_force_auth_user', None)
force_token = getattr(request, '_force_auth_token', None)
if force_user is not None or force_token is not None:
forced_auth = ForcedAuthentication(force_user, force_token)
self.authenticators = (forced_auth,)
不知道大家是否明白这段代码的意思,如果authenticators为真,则self.authenticators等于authenticators,如果authenticators为假,则self.authenticators等于一个空的元组
self.authenticators = authenticators or ()
我们这里要看下实例化Request这个类的时候,authenticators这个参数传递的是什么?
我们在回到initlize_request方法的返回值,下面我们要来看下self.get_authenticators()方法是在做什么
下面看下self.get_authenticators()这个方法的源码,从字面的我们就可以理解,self.authentication_classes是一个认证的类的列表。auth()是每个类的实例对象,这个方法的返回值就是列表,列表中的元素就是每个认证类的实例对象,这里先剧透一下,authentication_class这个属性是由我们自己的配置的
def get_authenticators(self):
"""
Instantiates and returns the list of authenticators that this view can use.
"""
return [auth() for auth in self.authentication_classes]
到这里,APIView类中的dispatch方法的initialize_request条线就做完了,就是给我们返回了一个新的Request类的实例,这个实例的authenticators就包括我们认证组件相关的类的实例对象
下面我们继续往下走APIView类的dispatch方法,走self.initial方法
11、下面先看下initial方法的源码
def initial(self, request, *args, **kwargs):
"""
Runs anything that needs to occur prior to calling the method handler.
"""
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)
12、我们这里来看下认证组件干了什么事情?进入认证组件perform_authentication方法。只返回一个request.user
def perform_authentication(self, request):
"""
Perform authentication on the incoming request. Note that if you override this and simply 'pass', then authentication
will instead be performed lazily, the first time either
`request.user` or `request.auth` is accessed.
"""
request.user
13、莫名其妙,返回一个实例的属性?其实这里大家不要忘记了,如果一个类的方法被property修饰了,调用这个方法的就可以使用属性的方式调用,而不用加括号了,如果大家不清楚,可以看我这篇博客:https://www.cnblogs.com/bainianminguo/p/9950607.html
14、下面我们看下request.user到底是什么?我们先要知道request是什么?看下面的截图
在dispatch方法中initial方法的参数有一个request,而这个request就是initialize_request的返回值,而initialize_request的返回值就是Request的实例对象
这个request有一个user的属性或者方法,我们下面来找下
15、下面我们来看下request.user到底是个什么东西?我们在Request类中确实找到了user这个方法,这个方法也被property装饰器装饰了,所以也印证了我们之前的猜测了,request.user是一个被property修饰的方法
@property
def user(self):
"""
Returns the user associated with the current request, as authenticated
by the authentication classes provided to the request.
"""
if not hasattr(self, '_user'):
with wrap_attributeerrors():
self._authenticate()
return self._user
16、然后看下self._authenticate方法
def _authenticate(self):
"""
Attempt to authenticate the request using each authentication instance
in turn.
"""
for authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate(self)
except exceptions.APIException:
self._not_authenticated()
raise if user_auth_tuple is not None:
self._authenticator = authenticator
self.user, self.auth = user_auth_tuple
return self._not_authenticated()
如果符合规范,则返回None,如果不符合规范,则raise抛出错误
到这里,我们就认证组件的源码梳理完了,下面我们来看下具体怎么写认证组件
17、下面进入如何写认证组件
我们的类中要有这么一个属性。
然后认证组件的类中要
18、做认证,我们是通过一个token来做的,每次用户登陆,我们都会给他重新一个token,然后把这个token告诉客户,下次客户来访问带着token,我们就认为认证通过了
所以我们先设计表,一个model表,一个Token表,两张表是一对一的关系
class User(models.Model):
name = models.CharField(max_length=32)
pwd = models.CharField(max_length=32) class Token(models.Model):
user = models.OneToOneField(to=User)
token = models.CharField(max_length=128)
19、然后我们写用户登陆的处理逻辑
from django.http import JsonResponse class LoginCBV(APIView):
def get(self,request):
pass def post(self,request):
name = request.data.get("name")
pwd = request.data.get("pwd") obj = models.User.objects.filter(name=name,pwd=pwd).exists()
res = {"code":200,"message":"","token":""}
if obj:
user_obj = models.User.objects.filter(name=name,pwd=pwd).first()
token = create_token(name)
models.Token.objects.update_or_create(user_obj,defaults={"token":token}) token_obj = models.Token.objects.get(user=user_obj)
res["token"] = token_obj.token else:
res["code"] = 201
res["message"] = "用户名或者密码错误"
import json
return JsonResponse(json.dumps(res))
上面的update_or_create的方法写错了,正确的写法是下面的写法
models.Token.objects.update_or_create(user=user_obj,defaults={"token":token})
20、这里还写了一个生成token的函数,加盐的盐为用户的名称
利用时间和用户的名称计算出来一个md5值,作为这次登陆的token
import hashlib
import time
def create_token(user):
now = time.time()
test_md5 = hashlib.md5(bytes(str(now),encoding="utf-8"))
test_md5.update(bytes(user,encoding="utf-8"))
return test_md5.hexdigest()
21、下面我们开始写的认证组件,如果我们想控制访问这条url:
url(r'^book_cbv/', views.Book_cbv.as_view(),name="test3"),
22、那么我们就需要进入Book_cbv这个类中来做操作,这个属性我们之前也看到了,名称必须是authentication,且值要为一个list
23、最后我们下Book_auther这个类
class Book_auther(BaseAuthentication):
def authenticate(self,request):
token = request.GET.get("token")
token_obj = models.Token.objects.filter(token=token).first()
if token_obj:
return token_obj.user.name,token_obj.token
else:
raise exceptions.AuthenticationFailed("验证失败")
def authenticate_header(self,request):
pass
24、最后我们用postman测试一下,首先先用post登陆一下,生成一下token
然后看下Token表中是否有token字段
25、我们再次用postman登录一下,看下token是否会更新,我们看到token已经更新
26、我们首先先不携带token去访问book表,看下效果,提示验证失败
27、下面我们携带token去访问,这样就可以返回查询到的结果了
大家要慢慢的体会
Django的rest_framework认证组件之局部设置源码解析的更多相关文章
- abp vnext2.0核心组件之.Net Core默认DI组件切换到AutoFac源码解析
老版Abp对Castle的严重依赖在vnext中已经得到了解决,vnext中DI容器可以任意更换,为了实现这个功能,底层架构相较于老版abp,可以说是进行了高度重构.当然这得益于.Net Core的D ...
- Django的rest_framework认证组件之全局设置源码解析
前言: 在我的上一篇博客我介绍了一下单独为某条url设置认证,但是如果我们想对所有的url设置认证,该怎么做呢?我们这篇博客就是给大家介绍一下在Rest_framework中如何实现全局的设置认证组件 ...
- Django之REST_FRAMEWORK 认证组件
Django之DRF之认证组件 # from rest_framework.views import APIView # APIView 中的 dispatch 中 执行的 self.initial( ...
- wtforms组件使用实例及源码解析
WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. WTforms作用:当网站中需要用到表单时,WTForms变得很有效.应该把表单定义为类,作为单独的一个模块. ...
- spring默认启动位置以及contextConfigLocation设置源码解析
这几天在看spring的源码,涉及到spring启动位置的部分,下面就看看spring到底是从哪儿开始加载的.本文使用的是spring3.0M3 首先spring的加载会借助一个监听器ContextL ...
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
- iOS富文本组件的实现—DTCoreText源码解析 渲染篇
本文转载至 http://blog.cnbang.net/tech/2729/ 上一篇介绍了DTCoreText怎样把HTML+CSS解析转换成NSAttributeString,本篇接着看看怎样把N ...
- Django settings源码解析
Django settings源码 Django中有两个配置文件 局部配置:配置文件settings.py,即项目同名文件夹下的settings.py文件 全局配置:django内部全局的配置文件se ...
- abp vnext2.0核心组件之领域实体组件源码解析
接着abp vnext2.0核心组件之模块加载组件源码解析和abp vnext2.0核心组件之.Net Core默认DI组件切换到AutoFac源码解析集合.Net Core3.1,基本环境已经完备, ...
随机推荐
- ios-密码判断
我们经常在项目时有涉及到用户或是手机号登录,这时一般会配合密码才能登录成功. 下面发一些关于手机和密码形式的判断: - (void)registButtonClick:(id)sender { )// ...
- Java day1
1. 学习java,首先是jdk的安装,JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+ ...
- axiso实战问题
this.axios({ method: 'get', url: '/api/projectmgt/project/Project/list', withCredentials : true, hea ...
- linux用ssh登录卡或者慢
原因:有可能是客户端在登录服务器时,服务器会先根据客户端的IP根据DNS去查找主机名,如果客户端的DNS服务器出现问题或者主机名有问题,就会卡一段时间 解决办法: # vi /etc/ssh/sshd ...
- [转帖][分享] 关于系统DIY--by 原罪
http://wuyou.net/forum.php?mod=viewthread&tid=399277&extra=page%3D1 前几天我发了一个帖子<Windows组件w ...
- Android批量验证渠道、版本号(Linux版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号(Linux版本)使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2../VerifyChannelVe ...
- 用到的linux命令
1.修改文件权限 chmod 777 文件路径 修改文件下所有文件权限 chmond -R 777 文件路径 2.修改文件 (保存文件的方法,在命令行窗口 shift+:换出底部命令行, q表示退出, ...
- docker容器内存占用 之 系统cache,docker下java的内存该如何配置
缘起: 监控(docker stats)显示容器内存被用完了,进入容器瞅了瞅,没有发现使用内存多的进程,使用awk等工具把容器所有进程使用的内存加起来看看,距离用完还远了去了,何故? 分析: 该不会d ...
- 《女神异闻录 5》的 UI 设计
转自:https://www.zhihu.com/question/50995871?sort=created <女神异闻录5>是近两年最为火热的JRPG游戏之一,它的出色不仅在于剧情暗讽 ...
- MYSQL SQL语句技巧初探(一)
MYSQL SQL语句技巧初探(一) 本文是我最近了解到的sql某些方法()组合实现一些功能的总结以后还会更新: rand与rand(n)实现提取随机行及order by原理的探讨. Bit_and, ...