django rest framework 从token中解析用户信息

from django.utils.deprecation import MiddlewareMixin
from rest_framework_jwt.utils import jwt_decode_handler

from utils import log


class SetUserSession(MiddlewareMixin):
    """
    把用户id放入session中,
    """
    def process_response(self, request, response):
        if request.path == "/login/":
            if response.status_code == 400:
                response.data["status_code"] = 400
                return response
            token = response.data["token"]
            user_dict = jwt_decode_handler(token=token)
            request.session["user_id"] = user_dict["user_id"]
            log.addlog(request, "登录成功")
        return response

 

你可能感兴趣的:(django,rest,framework)