当你发现自己的才华撑不起野心时,就请安静下来学习吧。

Personal site

↑点击传送

路由、认证

路由、认证

路由

	1 普通路由书写
        path('test/',views.TestAPIView.as_view()),
        re_path('test/(?P<pk>\d+)',views.TestDetailAPIView.as_view()),
	2 视图类继承ViewSetMinxin的路由,对应的请求方式执行对应的方法
        path('test2/',views.Test2View.as_view(actions={'get':'list','post':'create'})),
        re_path('test2/(?P<pk>\d+)',views.Test2DetailView.as_view(actions={'retrieve','put':'update','delete':'destory'})),
    3 视图类继承ModelViewSet自动生成路由,只有继承视图集的可以自动生成路由
        #导入路由对象
        from rest_framework.routers import SimpleRouter
        #实例化对象
        router = SimpleRouter()
        #注册
        router.register('test',views.Test)#test不需要加斜杠了
        #加入路由
        urlpatterns += router.urls

认证

#认证的写法
	1 写一个类,继承BaseAuthentication,重写authenticate,里面写逻辑,认证通过,返回两个值,一个值最终给了Requet对象的user,认证失败,抛异常:APIException或者AuthenticationFailed
    2 全局使用,局部使用
#认证源码分析
    #1 APIVIew----》dispatch方法---》self.initial(request, *args, **kwargs)---->有认证,权限,频率
    #2 只读认证源码: self.perform_authentication(request)
    #3 self.perform_authentication(request)就一句话:request.user,需要去drf的Request对象中找user属性(方法) 
    #4 Request类中的user方法,刚开始来,没有_user,走 self._authenticate()

    #5 核心,就是Request类的 _authenticate(self):
        def _authenticate(self):
            # 遍历拿到一个个认证器,进行认证
            # self.authenticators配置的一堆认证类产生的认证类对象组成的 list
            #self.authenticators 你在视图类中配置的一个个的认证类:authentication_classes=[认证类1,认证类2],对象的列表
            for authenticator in self.authenticators:
                try:
                    # 认证器(对象)调用认证方法authenticate(认证类对象self, request请求对象)
                    # 返回值:登陆的用户与认证的信息组成的 tuple
                    # 该方法被try包裹,代表该方法会抛异常,抛异常就代表认证失败
                    user_auth_tuple = authenticator.authenticate(self) #注意这self是request对象
                except exceptions.APIException:
                    self._not_authenticated()
                    raise

                # 返回值的处理
                if user_auth_tuple is not None:
                    self._authenticator = authenticator
                    # 如何有返回值,就将 登陆用户 与 登陆认证 分别保存到 request.user、request.auth
                    self.user, self.auth = user_auth_tuple
                    return
            # 如果返回值user_auth_tuple为空,代表认证通过,但是没有 登陆用户 与 登陆认证信息,代表游客
            self._not_authenticated()

认证组件的完整使用

#utils下的app01_auth.py
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed

from app01.models import UserToken

class MyAuthentication(BaseAuthentication):
    def authenticate(self, request):
        token = request.GET.get('token')
        if token:
            user_token = UserToken.objects.filter(token=token).first()
            if user_token:
                return user_token.user,token
            else:
                raise AuthenticationFailed('认证失败')
        else:
            raise AuthenticationFailed('请求地址需要携带token')
#全局配置,settings.py
	REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.app_auth.MyAuthentication"]
}
#局部配置,视图方法上方加上
	authentication_classes=[MyAuthentication]
#局部禁用,视图方法上方加上
	authentication_classes=[]
posted @ 2020-07-21 16:24  Joab-0429  阅读(156)  评论(0编辑  收藏  举报