1. 前言

为了反爬或限流节流,后端编写接口时,大部分 API 都会进行权限认证,只有认证通过,即:数据失常及未过期才会返回数据,否则间接报错

本篇文章以 Django 为例,聊聊后端 JWT 接口认证的操作流程

2. JWT 介绍
JWT 全称为 JSON Web Token,是目前支流的跨域认证解决方案

数据结构由 3 局部组成,两头由「 . 」宰割开

它们别离是:

  • Header 头部
  • Payload 负载
  • Signature 签名
# JWT 数据的格局# 组成形式:头部.负载.签名Header.Payload.Signature

其中

Header 用于设置签名算法及令牌类型,默认签名算法为 「 HS256 」,令牌类型能够设置为「 JWT 」

Payload 用于设置须要传递的数据,蕴含:iss 签发人、exp 过期工夫、iat 签发工夫等

Signature 用于对 Header 和 Payload 进行签名,默认应用的签名算法为 Header 中指定的算法

# JWT 数据组成# Header. Payload. Signature# Header:{ "alg": "HS256","typ": "JWT"}# Payload:iss、exp、iat等# Signature:签名Signature = HMACSHA256(  base64UrlEncode(header) + "." +  base64UrlEncode(payload),  secret)

PS:base64UrlEncode 相比 Base64 算法,会将后果中的「 = 」省略、「 + 」替换成「 - 」、「 / 」替换成「 _ 」

3. 实战一下
首先,在虚拟环境中装置 JWT 依赖包

# 装置jwt依赖包pip3 install pyjwt

而后,定义一个办法用于生成 JWT Token

须要留神的是,生成 JWT Token 时须要指定过期工夫、加密形式等

import timeimport jwtfrom django.conf import settingsdef generate_jwt_token(user):    """    生成一个JWT Token    :param user:    :return:    """    # 设置token的过期工夫戳    # 比方:设置7天过期    timestamp = int(time.time()) + 60 * 60 * 24 * 7    # 加密生成Token    # 加密形式:HS256    return jwt.encode({"userid": user.pk, "exp": timestamp}, settings.SECRET_KEY,'HS256')

接着,编写一个认证类

该类继承于「 BaseAuthentication 」基类,重写外部函数「 authenticate() 」,对申请参数进行 JWT 解密,并进行数据库查问,只有认证通过才返回数据,否则抛出异样

import timeimport jwtfrom django.conf import settingsfrom django.contrib.auth import get_user_modelfrom rest_framework import exceptionsfrom rest_framework.authentication import BaseAuthentication, get_authorization_headerUser = get_user_model()class JWTAuthentication(BaseAuthentication):    """自定义认证类"""    keyword = 'jwt'    model = None    def get_model(self):        if self.model is not None:            return self.model        from rest_framework.authtoken.models import Token        return Token    """    A custom token model may be used, but must have the following properties.    * key -- The string identifying the token    * user -- The user to which the token belongs    """    def authenticate(self, request):        auth = get_authorization_header(request).split()        if not auth or auth[0].lower() != self.keyword.lower().encode():            return None        if len(auth) !=2:            raise exceptions.AuthenticationFailed("认证异样!")        # jwt解码        try:            jwt_token = auth[1]            jwt_info = jwt.decode(jwt_token, settings.SECRET_KEY,'HS256')            # 获取userid            userid = jwt_info.get("userid")            # 查问用户是否存在            try:                user = User.objects.get(pk=userid)                return user, jwt_token            except Exception:                raise exceptions.AuthenticationFailed("用户不存在")        except jwt.ExpiredSignatureError:            raise exceptions.AuthenticationFailed("道歉,该token已过期!")

最初,在视图集 ViewSet 中,只须要在属性「 authentication_classes 」中指定认证列表即可

from rest_framework import viewsetsfrom .models import *from .serializers import *from .authentications import *class GoodsViewSet(viewsets.ModelViewSet):    # 所有商品数据    queryset = Goods.objects.all()    # 序列化    serializer_class = GoodsSerializer    # JWT受权    authentication_classes = [JWTAuthentication]

4. 最初
在理论我的项目中,个别在登录的时候生成 JWT Token,后续接口中只须要在申请头中设置 JWT Token 即可失常返回数据

import requestsurl = "***.***.****"payload={}headers = {  'AUTHORIZATION': 'jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiJVTmJCRTJTRlNndm5DU0c3amdQZGJVIiwiZXhwIjoxNjI2MDk5NDA5fQ.cxXsRulEWWQotNpb7XwlZbISrrpb7rSRCjkLsyb8WDM'}response = requests.request("GET", url, headers=headers, data=payload)print(response.text)

最近整顿了几百 G 的 Python 学习材料,蕴含新手入门电子书、教程、源码等等,收费分享给大家!想要的返回 “Python 编程学习圈”,发送 “J” 即可收费取得