update code

master
bettleChen 1 year ago
parent ce596c8c3f
commit 4aaf96856c

@ -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)

Loading…
Cancel
Save