• 路由、认证


    路由、认证

    路由

    	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=[]
    
    作者:linqiaobao
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    博客园添加访问人数统计【转】
    Android环境下通过C框架层控制WIFI【转】
    用户态文件系统fuse学习【转】
    linux内核 RCU机制详解【转】
    使用diff制作补丁【学习笔记】
    OAuth2授权原理
    Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
    lock关键字只不过是C#提供的语法糖
    关于OATUH中的AUTHRAZITON CODE和TOKEN的关系,实际上就是这么回事
    SQL Server 索引设计指南
  • 原文地址:https://www.cnblogs.com/linqiaobao/p/13355786.html
Copyright © 2020-2023  润新知