From 4aaf96856cf51a04126053d080128aeb266b8e62 Mon Sep 17 00:00:00 2001 From: bettleChen <2207153529@qq.com> Date: Wed, 13 Sep 2023 09:47:19 +0800 Subject: [PATCH] update code --- EduSystemServer/API/middle.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/EduSystemServer/API/middle.py b/EduSystemServer/API/middle.py index 71f0640..183a31f 100644 --- a/EduSystemServer/API/middle.py +++ b/EduSystemServer/API/middle.py @@ -2,6 +2,9 @@ import jwt from django.http import JsonResponse from EduSystemServer.settings import TOKEN_KEY from EduSystemServer.utils import ResponseUtil +from Eduadmin.models import Admin +from Student.models import Student +from teacher.models import Teacher class JWTMiddleware: @@ -19,6 +22,15 @@ class JWTMiddleware: # 验证 Token payload = jwt.decode(token, TOKEN_KEY, algorithms=['HS256']) # 将解码后的 Token 数据存储在 request 中,以便视图可以访问 + if payload.get("type") == "student": + if not Student.objects.filter(username=payload.get("username")).exists(): + return JsonResponse(ResponseUtil.error("登录失效!")) + if payload.get("type") == "teacher": + if not Teacher.objects.filter(username=payload.get("username")).exists(): + return JsonResponse(ResponseUtil.error("登录失效!")) + if payload.get("type") == "admin": + if not Admin.objects.filter(username=payload.get("username")).exists(): + return JsonResponse(ResponseUtil.error("登录失效!")) request.jwt_payload = payload except jwt.ExpiredSignatureError: return JsonResponse(ResponseUtil.error("登录失效!"), status=401)