<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          5 分鐘,快速入門 Python JWT 接口認(rèn)證

          共 6499字,需瀏覽 13分鐘

           ·

          2021-07-10 06:13




          點(diǎn)擊上方“AirPython”,選擇“加為星標(biāo)

          第一時(shí)間關(guān)注 Python 原創(chuàng)干貨!


          1. 前言

          大家好,我是安果!

          為了反爬或限流節(jié)流,后端編寫接口時(shí),大部分 API 都會(huì)進(jìn)行權(quán)限認(rèn)證,只有認(rèn)證通過(guò),即:數(shù)據(jù)正常及未過(guò)期才會(huì)返回?cái)?shù)據(jù),否則直接報(bào)錯(cuò)

          本篇文章以 Django 為例,聊聊后端 JWT 接口認(rèn)證的操作流程

          2. JWT 介紹

          JWT 全稱為 JSON Web Token,是目前主流的跨域認(rèn)證解決方案

          數(shù)據(jù)結(jié)構(gòu)由 3 部分組成,中間由「 . 」分割開

          它們分別是:

          • Header 頭部

          • Payload 負(fù)載

          • Signature 簽名

          # JWT 數(shù)據(jù)的格式
          # 組成方式:頭部.負(fù)載.簽名
          Header.Payload.Signature

          其中

          Header 用于設(shè)置簽名算法及令牌類型,默認(rèn)簽名算法為 「 HS256 」,令牌類型可以設(shè)置為「 JWT

          Payload 用于設(shè)置需要傳遞的數(shù)據(jù),包含:iss 簽發(fā)人、exp 過(guò)期時(shí)間、iat 簽發(fā)時(shí)間等

          Signature 用于對(duì) Header 和 Payload 進(jìn)行簽名,默認(rèn)使用的簽名算法為 Header 中指定的算法

          # JWT 數(shù)據(jù)組成
          # Header. Payload. Signature
          # Header:{ "alg""HS256","typ""JWT"}
          # Payload:iss、exp、iat等
          # Signature:簽名
          Signature = HMACSHA256(
            base64UrlEncode(header) + "." +
            base64UrlEncode(payload),
            secret)

          PS:base64UrlEncode 相比 Base64 算法,會(huì)將結(jié)果中的「 = 」省略、「 + 」替換成「 - 」、「 / 」替換成「 _ 」

          3. 實(shí)戰(zhàn)一下

          首先,在虛擬環(huán)境中安裝 JWT 依賴包

          # 安裝jwt依賴包
          pip3 install pyjwt

          然后,定義一個(gè)方法用于生成 JWT Token

          需要注意的是,生成 JWT Token 時(shí)需要指定過(guò)期時(shí)間、加密方式等

          import time
          import jwt
          from django.conf import settings

          def generate_jwt_token(user):
              """
              生成一個(gè)JWT Token
              :param user:
              :return:
              """

              # 設(shè)置token的過(guò)期時(shí)間戳
              # 比如:設(shè)置7天過(guò)期
              timestamp = int(time.time()) + 60 * 60 * 24 * 7

              # 加密生成Token
              # 加密方式:HS256
              return jwt.encode({"userid": user.pk, "exp": timestamp}, settings.SECRET_KEY,'HS256')

          接著,編寫一個(gè)認(rèn)證類

          該類繼承于「 BaseAuthentication 」基類,重寫內(nèi)部函數(shù)「 authenticate() 」,對(duì)請(qǐng)求參數(shù)進(jìn)行 JWT 解密,并進(jìn)行數(shù)據(jù)庫(kù)查詢,只有認(rèn)證通過(guò)才返回?cái)?shù)據(jù),否則拋出異常

          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):
              """自定義認(rèn)證類"""

              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("認(rèn)證異常!")

                  # 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已過(guò)期!")

          最后,在視圖集 ViewSet 中,只需要在屬性「 authentication_classes 」中指定認(rèn)證列表即可

          from rest_framework import viewsets
          from .models import *
          from .serializers import *
          from .authentications import *

          class GoodsViewSet(viewsets.ModelViewSet):
              # 所有商品數(shù)據(jù)
              queryset = Goods.objects.all()

              # 序列化
              serializer_class = GoodsSerializer

              # JWT授權(quán)
              authentication_classes = [JWTAuthentication]

          4. 最后

          在實(shí)際項(xiàng)目中,一般在登錄的時(shí)候生成 JWT Token,后續(xù)接口中只需要在請(qǐng)求頭中設(shè)置 JWT Token 即可正常返回?cái)?shù)據(jù)

          import requests

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

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

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

          如果你覺得文章還不錯(cuò),請(qǐng)大家 點(diǎn)贊、分享、留言 下,因?yàn)檫@將是我持續(xù)輸出更多優(yōu)質(zhì)文章的最強(qiáng)動(dòng)力!



          ??分享、點(diǎn)贊、在看,給個(gè)三連擊唄!
          瀏覽 55
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日日日日人人人夜夜2022 | 久久久久久蜜桃 | 五月天国产视频乱码免费 | 日本精品无码a 6 2v在线 | 欧美 亚洲 日韩 国产 高清 |