5 分鐘,快速入門 Python JWT 接口認證

語言: CN / TW / HK

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 time
import jwt
from django.conf import settings

def 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 time

import jwt
from django.conf import settings
from django.contrib.auth import get_user_model
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication, get_authorization_header

User = 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 viewsets
from .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 requests

url = "***.***.****"

payload={}
headers = {
  'AUTHORIZATION': 'jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiJVTmJCRTJTRlNndm5DU0c3amdQZGJVIiwiZXhwIjoxNjI2MDk5NDA5fQ.cxXsRulEWWQotNpb7XwlZbISrrpb7rSRCjkLsyb8WDM'
}

response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)

以上就是本次分享的所有內容,如果你覺得文章還不錯,歡迎關注公眾號:Python編程學習圈,每日干貨分享,內容覆蓋Python電子書、教程、數據庫編程、Django,爬蟲,雲計算等等。或是前往編程學習網,瞭解更多編程技術知識。