DRF-Django rest framework
认证权限频率
1. 认证Authentication
1. DRF认证功能介绍
1. 认证:判断用户是否登录到系统中
登录功能:(一个大表拆成一对一的两个小表,其实叫垂直分表)
-update_or_create()
2. 后期基本上会用JWT的认证
3. 自定制的认证
2. 认证功能源码分析
1. 源码分析
'''
所有drf的请求,都会执行APIView的dispatch
-Request对象中有个authenticators属性,它是我们在视图类中配置的认证类的对象,放到了列表中
-只要通过了认证
request.user # 就是当前登录用户,在认证类中返回的
'''
APIView ==> dispatch
==> self.initial(request, *args, **kwargs)
==> self.perform_authentication(request)
==> Request.user
==> self._authenticate(self): Request类的方法
==> self.authenticators:Request类的属性
==> 在Request对象实例化的时候传入的
==> Request在什么时候实例化的: dispatch的时候
==> APIView:self.get_authenticators()
==> return [auth() for auth in self.authentication_classes]
==> 如果在自己定义的视图类中写了authentication_classes=[类1,类2]
==> Request的self.authenticators就变成了我们配置的一个个类的对象
2. self._authenticate(self): Request类的方法
def _authenticate(self):
for authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate