from django.http import JsonResponse class AuthMiddleware: """ 验证权限登录中间件 """ def __init__(self, get_response): self.get_response = get_response def __call__(self, request): path = request.path if path in [ "/login", ]: response = self.get_response(request) return response session = request.session if not session.get("username") and not session.get("type"): return JsonResponse({"code": -1, "msg": "not login!"}, status=401) response = self.get_response(request) return response