""" 1) 服务器不要存储token,token交给每个客户端本身存储,服务器压力小 2)服务器存储的是 签发和校验token 两段算法,签发认证的效率高 3)算法完成各集群服务器同步成本低,路由项目完成集群部署(适应高并发) """
""" 1) jwt token采用三段式:头部.载荷.签名 2)每一部分都是一个json字典加密形参的字符串 3)头部和载荷采用的是base64可逆加密(前台后台均可以解密) 4)签名采用hash256不可逆加密(后台校验采用碰撞校验) 5)各部分字典的内容: 头部:基础信息 - 公司信息、项目组信息、可逆加密采用的算法 载荷:有用但非私密的信息 - 用户可公开信息、过时时间 签名:头部+载荷+秘钥 不可逆加密后的结果 注:服务器jwt签名加密秘钥必定不能泄露 签发token:固定的头部信息加密.当前的登录用户与过时时间加密.头部+载荷+秘钥生成不可逆加密 校验token:头部可校验也能够不校验,载荷校验出用户与过时时间,头部+载荷+秘钥完成碰撞检测校验token是否被篡改 """
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()
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()响应git
点击ObtainJSONWebToken,发现里面没写东西,走父类JSONWebTokenAPIView,没有get方法github
只有post方法web
post方法中的serializer反序列化中校验的算法
若是密码正确,返回token数据库
看点切分,有三段,反复请求第一段不会变,第二段会变几个字母也就是只是过时时间日后移的时间变化,第三段疯狂变化django
换了登陆用户第二段就变化大了json
在数据库api_user表中添加数据api
首段就算是不一样的用户都不会改变缓存
源码: class JSONWebTokenAPIView(APIView): """ Base API View that various JWT interactions inherit from. """ #将认证和权限禁用 #登陆必定不能有认证和权限 permission_classes = () authentication_classes = ()
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:\ten_django\jwt E:\ten_django\rate_throttle