zoukankan      html  css  js  c++  java
  • jwt

    认证规则图

    django不分离

    drf分类

    认证规则演变图

    数据库session认证:低效

    缓存认证:高效

    jwt认证:高效

    缓存认证:不易并发

    jwt认证:易并发

    JWT认证

    优点
    """
    1) 服务器不要存储token,token交给每一个客户端自己存储,服务器压力小
    2)服务器存储的是 签发和校验token 两段算法,签发认证的效率高
    3)算法完成各集群服务器同步成本低,路由项目完成集群部署(适应高并发)
    """
    
    格式
    """
    1) jwt token采用三段式:头部.载荷.签名
    2)每一部分都是一个json字典加密形参的字符串
    3)头部和载荷采用的是base64可逆加密(前台后台都可以解密)
    4)签名采用hash256不可逆加密(后台校验采用碰撞校验)
    5)各部分字典的内容:
    	头部:基础信息 - 公司信息、项目组信息、可逆加密采用的算法
    	载荷:有用但非私密的信息 - 用户可公开信息、过期时间
    	签名:头部+载荷+秘钥 不可逆加密后的结果
    	注:服务器jwt签名加密秘钥一定不能泄露
    	
    签发token:固定的头部信息加密.当前的登陆用户与过期时间加密.头部+载荷+秘钥生成不可逆加密
    校验token:头部可校验也可以不校验,载荷校验出用户与过期时间,头部+载荷+秘钥完成碰撞检测校验token是否被篡改
    """
    

    drf-jwt插件

    官网
    https://github.com/jpadilla/django-rest-framework-jwt
    
    安装
    >: pip install djangorestframework-jwt
        
        
    解释:    
    from djangorestframework-jwt
    点击djangorestframework-jwt 定位rest_framework_jwt文件夹中
    查找有models.py,但是里面什么都没写,所以说它没有数据库,所以不需要注册
    我们只需要导入文件直接使用
    
    而rest_framework这个模块就需要注册,因为数据库有数据
    
    rest_framework_jwt包中,我们要使用的就是authentication.py和views.py
    views.py中的类就是在我们自定义类时要继承的类
    

    # ObtainJSONWebToken视图类就是通过username和password得到user对象然后签发token
    #通过账号密码签发token,依赖auth组件的RBAC用户权限六表
    class ObtainJSONWebToken(JSONWebTokenAPIView):
        """
        API View that receives a POST with a user's username and password.
    
        Returns a JSON Web Token that can be used for authenticated requests.
        """
        serializer_class = JSONWebTokenSerializer
        
     #校验token,如果通过原样返回(没啥用)
    class VerifyJSONWebToken(JSONWebTokenAPIView):
        """
        API View that checks the veracity of a token, returning the token if it
        is valid.
        """
        serializer_class = VerifyJSONWebTokenSerializer
    
     
    #根据一个合法的token,返回刷新后的token
    class RefreshJSONWebToken(JSONWebTokenAPIView):
        """
        API View that returns a refreshed token (with new expiration) based on
        existing token
    
        If 'orig_iat' field (original issued-at-time) is found, will first check
        if it's within expiration window, then copy it to the new token
        """
        serializer_class = RefreshJSONWebTokenSerializer
        
        
    三个函数:
    obtain_jwt_token = ObtainJSONWebToken.as_view()
    refresh_jwt_token = RefreshJSONWebToken.as_view()
    verify_jwt_token = VerifyJSONWebToken.as_view()
    
    登录 - 签发token:api/urls.py
    
    from rest_framework_jwt.views import ObtainJSONWebToken, obtain_jwt_token
    urlpatterns = [
        # url(r'^jogin/$', ObtainJSONWebToken.as_view()),
        url(r'^jogin/$', obtain_jwt_token),
    ]
    
    #只要配置了路由就可以做测试了
    

    发送get请求,url(r'^jogin/$', obtain_jwt_token) 也就是ObtainJSONWebToken.as_view()响应

    点击ObtainJSONWebToken,发现里面没写东西,走父类JSONWebTokenAPIView,没有get方法

    只有post方法

    post方法中的serializer反序列化中校验的

    如果密码正确,返回token

    看点切分,有三段,反复请求第一段不会变,第二段会变几个字母也就是只是过期时间往后移的时间变化,第三段疯狂变化

    换了登录用户第二段就变化大了

    在数据库api_user表中添加数据

    首段就算是不同的用户都不会改变

    源码:
    class JSONWebTokenAPIView(APIView):
        """
        Base API View that various JWT interactions inherit from.
        """
        #将认证和权限禁用
        #登录一定不能有认证和权限
        permission_classes = ()
        authentication_classes = ()
    
    认证 - 校验token:全局或局部配置drf-jwt的认证类 JSONWebTokenAuthentication
    from rest_framework.views import APIView
    from utils.response import APIResponse
    # 必须登录后才能访问 - 通过了认证权限组件
    from rest_framework.permissions import IsAuthenticated
    from rest_framework_jwt.authentication import JSONWebTokenAuthentication
    class UserDetail(APIView):
        authentication_classes = [JSONWebTokenAuthentication]  # jwt-token校验request.user
        permission_classes = [IsAuthenticated]  # 结合权限组件筛选掉游客
        def get(self, request, *args, **kwargs):
            return APIResponse(results={'username': request.user.username})
    
    路由与接口测试
    # 路由
    url(r'^user/detail/$', views.UserDetail.as_view()),
    
    # 接口:/api/user/detail/
    # 认证信息:必须在请求头的 Authorization 中携带 "jwt 后台签发的token" 格式的认证字符串
    
    测试

    jwt和频率认证的代码详见
    E: en_djangojwt
    E: en_django ate_throttle

  • 相关阅读:
    php 克隆和引用类
    php 抽象类、接口和构析方法
    php 面向对象之继承、多态和静态方法
    php封装练习
    php 面向对象之封装
    php 简单操作数据库
    php 练习
    用php输入表格内容
    php 指针遍历、预定义数组和常用函数
    php 数组定义、取值和遍历
  • 原文地址:https://www.cnblogs.com/huangxuanya/p/11723389.html
Copyright © 2011-2022 走看看