zoukankan      html  css  js  c++  java
  • 重写身份认证

    - 身份认证
    from rest_framework.authentication import BaseAuthentication
    - 导入自定义算法解析token
    from utils.TokenIssue import Token_Issue
    
    class NewBaseAuthentication(BaseAuthentication):
        def authenticate(self, request):
       
            - 认证模块工作原理:
            1)继承BaseAuthentication类,重写authenticate方法
            2)认证规则(authenticate方法实现体):
                没有携带认证信息,直接返回None => 游客
                有认证信息,校验失败,抛异常 => 非法用户
                有认证信息,校验出User对象 => 合法用户
    
            - 验证token
            - 1. 拿到前台token
            token = request.META.get('HTTP_AUTHORIZATION')
            if not token: # 没有就是游客模式呗
                return None
            - 解析token
            issue = Token_Issue()
            - 调用自定义密码加密解密算法,传进去token, 返回一个对象
            user_obj = issue.resolve_token(token)
    
            - 4. 检验成功返回一个元组(user_obj, token)
            return (user_obj, token)
    
    
    
    REST_FRAMEWORK = {
       
        # 认证模块
        'DEFAULT_AUTHENTICATION_CLASSES': [
            - 游客模式,不验证
            'rest_framework.authentication.SessionAuthentication',
            - 验证有token的时候
            'rest_framework.authentication.BasicAuthentication',
            - 自定义认证类
            'utils.authentications.NewBaseAuthentication'
        ],
      
    }
    
  • 相关阅读:
    ios开发 MJExtension
    ios开发 time profile用法
    ios开发 UIApplication
    ios AFNetWorking
    ios开发 为什么NSString、NSArray、NSDIctionary用copy修饰
    ios动画
    ios开发GCD
    重要链接
    记录,员工关心的内容。
    怎样用 Wise Installation System 制作汉化补丁?(转)
  • 原文地址:https://www.cnblogs.com/xiongchao0823/p/11930858.html
Copyright © 2011-2022 走看看