1. 程式人生 > 實用技巧 >jwt 相關

jwt 相關

  前言:jwt是一個開放標準(RFC 7519),它定義了一種緊湊的、自包含的方式,用於作為JSON物件在各方之間安全地傳輸資訊。
該資訊可以被驗證和信任,因為它是數字簽名的.

1.jwt 認證的優點:
1) 伺服器不要儲存token,token交給每一個客戶端自己儲存,伺服器壓力小 2)伺服器儲存的是 簽發和校驗token 兩段演算法,簽發認證的效率高 3)演算法完成各叢集伺服器同步成本低,路由專案完成叢集部署(適應高併發) 2.jwt 的構成: 1) jwt token採用三段式:頭部.載荷.簽名 2)每一部分都是一個json字典加密形參的字串
3)頭部和載荷採用的是base64可逆加密(前臺後臺都可以解密) 4)簽名採用hash256不可逆加密(後臺校驗採用碰撞校驗) 5)各部分字典的內容: 頭部:基礎資訊 - 公司資訊、專案組資訊、可逆加密採用的演算法 載荷:有用但非私密的資訊 - 使用者可公開資訊、過期時間 簽名:頭部+載荷+祕鑰 不可逆加密後的結果 注:伺服器jwt簽名加密祕鑰一定不能洩露 簽發token:固定的頭部資訊加密.當前的登陸使用者與過期時間加密.頭部+載荷+祕鑰生成不可逆加密 校驗token:頭部可校驗也可以不校驗,載荷校驗出使用者與過期時間,頭部
+載荷+祕鑰完成碰撞檢測校驗token是否被篡改 3.安裝: pip install djangorestframework-jwt
登入 - 簽發token:api/urls.py 
# ObtainJSONWebToken檢視類就是通過username和password得到user物件然後簽發token from rest_framework_jwt.views import ObtainJSONWebToken, obtain_jwt_token urlpatterns = [ url(r'^jogin/$', ObtainJSONWebToken.as_view()), url(r
'^jogin/$', obtain_jwt_token), # 認證資訊:必須在請求頭的 Authorization 中攜帶 "jwt 後臺簽發的token" 格式的認證字串 url(r'^user/detail/$', views.UserDetail.as_view()), ] 認證 - 校驗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})
簽發token

前提:給一個區域性禁用了所有 認證與許可權 的檢視類傳送使用者資訊得到token,其實就是登入介面

1)rest_framework_jwt.views.ObtainJSONWebToken 的 父類 JSONWebTokenAPIView 的 post 方法       接受有username、password的post請求2)post方法將請求資料交給 rest_framework_jwt.serializer.JSONWebTokenSerializer 處理        
    完成資料的校驗,會走序列化類的 全域性鉤子校驗規則,校驗得到登入使用者並簽發token儲存在序列化物件中
核心:rest_framework_jwt.serializer.JSONWebTokenSerializer的validate(self, attrs)方法 def validate(self, attrs): # 賬號密碼字典 credentials = { self.username_field: attrs.get(self.username_field), 'password': attrs.get('password') } if all(credentials.values()): # 簽發token第1步:用賬號密碼得到user物件 user = authenticate(**credentials) if user: if not user.is_active: msg = _('User account is disabled.') raise serializers.ValidationError(msg) # 簽發token第2步:通過user得到payload,payload包含著使用者資訊與過期時間 payload = jwt_payload_handler(user) # 在檢視類中,可以通過 序列化物件.object.get('user'或者'token') 拿到user和token return { # 簽發token第3步:通過payload簽發出token 'token': jwt_encode_handler(payload), 'user': user } else: msg = _('Unable to log in with provided credentials.') raise serializers.ValidationError(msg) else: msg = _('Must include "{username_field}" and "password".') msg = msg.format(username_field=self.username_field) raise serializers.ValidationError(msg)
手動簽發token邏輯

 1)通過username、password得到user物件
 2)通過user物件生成payload:jwt_payload_handler(user) => payload
        from rest_framework_jwt.serializers import jwt_payload_handler
 3)通過payload簽發token:jwt_encode_handler(payload) => token
        from rest_framework_jwt.serializers import jwt_encode_handler
校驗token

前提:訪問一個配置了jwt認證規則的檢視類,就需要提交認證字串token,在認證類中完成token的校驗

1)rest_framework_jwt.authentication.JSONWebTokenAuthentication 的 父類 BaseJSONWebTokenAuthentication的
authenticate 方法
請求頭拿認證資訊jwt-token => 通過反爬小規則確定有用的token => payload => user 核心:est_framework_jwt.authentication.BaseJSONWebTokenAuthentication的authenticate(self, request)方法 def authenticate(self, request): """ Returns a two-tuple of `User` and token if a valid signature has been supplied using JWT-based authentication. Otherwise returns `None`. """ # 帶有反爬小規則的獲取token:前臺必須按 "jwt token字串" 方式提交 # 校驗user第1步:從請求頭 HTTP_AUTHORIZATION 中拿token,並提取 jwt_value = self.get_jwt_value(request) # 遊客 if jwt_value is None: return None # 校驗 try: # 校驗user第2步:token => payload payload = jwt_decode_handler(jwt_value) except jwt.ExpiredSignature: msg = _('Signature has expired.') raise exceptions.AuthenticationFailed(msg) except jwt.DecodeError: msg = _('Error decoding signature.') raise exceptions.AuthenticationFailed(msg) except jwt.InvalidTokenError: raise exceptions.AuthenticationFailed() # 校驗user第3步:token => payload user = self.authenticate_credentials(payload) return (user, jwt_value)
手動校驗token邏輯

 1)從請求頭中獲取token
 2)根據token解析出payload:jwt_decode_handler(token) => payloay
        from rest_framework_jwt.authentication import jwt_decode_handler
 3)根據payload解析出user:self.authenticate_credentials(payload) => user
        繼承drf-jwt的BaseJSONWebTokenAuthentication,拿到父級的authenticate_credentials方法
案例:實現多方式登陸簽發token
from django.db import models

from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True)

    class Meta:
        db_table = 'api_user'
        verbose_name = '使用者表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username
models.py
from rest_framework import serializers
from . import models
import re

# 拿到前臺token的兩個函式: user => payload => token
# from rest_framework_jwt.settings import api_settings
# jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
# jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
from rest_framework_jwt.serializers import jwt_payload_handler
from rest_framework_jwt.serializers import jwt_encode_handler


# 1) 前臺提交多種登入資訊都採用一個key,所以後臺可以自定義反序列化欄位進行對應
# 2) 序列化類要處理序列化與反序列化,要在fields中設定model繫結的Model類所有使用到的欄位
# 3) 區分序列化欄位與反序列化欄位 read_only | write_only
# 4) 在自定義校驗規則中(區域性鉤子、全域性鉤子)校驗資料是否合法、確定登入的使用者、根據使用者簽發token
# 5) 將登入的使用者與簽發的token儲存在序列化類物件中
class UserModelSerializer(serializers.ModelSerializer):
    # 自定義反序列欄位:一定要設定write_only,只參與反序列化,不會與model類欄位對映
    usr = serializers.CharField(write_only=True)
    pwd = serializers.CharField(write_only=True)
    class Meta:
        model = models.User
        fields = ['usr', 'pwd', 'username', 'mobile', 'email']
        # 系統校驗規則
        extra_kwargs = {
            'username': {
                'read_only': True
            },
            'mobile': {
                'read_only': True
            },
            'email': {
                'read_only': True
            },
        }

    def validate(self, attrs):
        usr = attrs.get('usr')
        pwd = attrs.get('pwd')

        # 多方式登入:各分支處理得到該方式下對應的使用者
        if re.match(r'.+@.+', usr):
            user_query = models.User.objects.filter(email=usr)
        elif re.match(r'1[3-9][0-9]{9}', usr):
            user_query = models.User.objects.filter(mobile=usr)
        else:
            user_query = models.User.objects.filter(username=usr)
        user_obj = user_query.first()

        # 簽發:得到登入使用者,簽發token並存儲在例項化物件中
        if user_obj and user_obj.check_password(pwd):
            # 簽發token,將token存放到 例項化類物件的token 名字中
            payload = jwt_payload_handler(user_obj)
            token = jwt_encode_handler(payload)
            # 將當前使用者與簽發的token都儲存在序列化物件中
            self.user = user_obj
            self.token = token
            return attrs

        raise serializers.ValidationError({'data': '資料有誤'})
serializers.py
#實現多方式登陸簽發token:賬號、手機號、郵箱等登陸
# 1) 禁用認證與許可權元件
# 2) 拿到前臺登入資訊,交給序列化類
# 3) 序列化類校驗得到登入使用者與token存放在序列化物件中
# 4) 取出登入使用者與token返回給前臺
import re
from . import serializers, models
from utils.response import APIResponse

from rest_framework_jwt.serializers import jwt_payload_handler
from rest_framework_jwt.serializers import jwt_encode_handler

class LoginAPIView(APIView):
    # 1) 禁用認證與許可權元件
    authentication_classes = []
    permission_classes = []
    def post(self, request, *args, **kwargs):
        # 2) 拿到前臺登入資訊,交給序列化類,規則:賬號用usr傳,密碼用pwd傳
        user_ser = serializers.UserModelSerializer(data=request.data)
        # 3) 序列化類校驗得到登入使用者與token存放在序列化物件中
        user_ser.is_valid(raise_exception=True)
        # 4) 取出登入使用者與token返回給前臺
        return APIResponse(token=user_ser.token, results=serializers.UserModelSerializer(user_ser.user).data)

    # "一根筋" 思考方式:所有邏輯都在檢視類中處理
    def my_post(self, request, *args, **kwargs):
        usr = request.data.get('usr')
        pwd = request.data.get('pwd')
        if re.match(r'.+@.+', usr):
            user_query = models.User.objects.filter(email=usr)
        elif re.match(r'1[3-9][0-9]{9}', usr):
            user_query = models.User.objects.filter(mobile=usr)
        else:
            user_query = models.User.objects.filter(username=usr)
        user_obj = user_query.first()
        if user_obj and user_obj.check_password(pwd):
            payload = jwt_payload_handler(user_obj)
            token = jwt_encode_handler(payload)
            return APIResponse(results={'username': user_obj.username}, token=token)
        return APIResponse(data_msg='不可控錯誤')
views.py

案例:自定義認證反爬規則的認證類
import jwt
from rest_framework_jwt.authentication import BaseJSONWebTokenAuthentication
from rest_framework_jwt.authentication import jwt_decode_handler
from rest_framework.exceptions import AuthenticationFailed
class JWTAuthentication(BaseJSONWebTokenAuthentication):
    def authenticate(self, request):
        jwt_token = request.META.get('HTTP_AUTHORIZATION')

        # 自定義校驗規則:auth token jwt
        token = self.parse_jwt_token(jwt_token)

        if token is None:
            return None

        try:
            # token => payload
            payload = jwt_decode_handler(token)
        except jwt.ExpiredSignature:
            raise AuthenticationFailed('token已過期')
        except:
            raise AuthenticationFailed('非法使用者')
        # payload => user
        user = self.authenticate_credentials(payload)

        return (user, token)

    # 自定義校驗規則:auth token jwt,auth為前鹽,jwt為後鹽
    def parse_jwt_token(self, jwt_token):
        tokens = jwt_token.split()
        if len(tokens) != 3 or tokens[0].lower() != 'auth' or tokens[2].lower() != 'jwt':
            return None
        return tokens[1]
authentications.py
from rest_framework.views import APIView
from utils.response import APIResponse
# 必須登入後才能訪問 - 通過了認證許可權元件
from rest_framework.permissions import IsAuthenticated
# 自定義jwt校驗規則
from .authentications import JWTAuthentication
class UserDetail(APIView):
    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    def get(self, request, *args, **kwargs):
        return APIResponse(results={'username': request.user.username})
views.py