自定义Django_rest_framework_jwt登陆错误返回

自定义Django_rest_framework_jwt登陆错误返回

文章 :这位博主的方法是对的,但是修改源码,个人是非常不赞同的,我就自己按照这位博主重写了登录错误返回

# urls.py
# 这是我们的路由

from rest_framework_jwt import views

urlpatterns = [
    # 获取
    path('api-token-auth/', views.obtain_jwt_token),

    # 刷新
    path('api-token-refresh/', views.refresh_jwt_token),

    # 校验
    path('api-token-verify/', views.verify_jwt_token),
]

我发现jwt的返回都是由JSONWebTokenAPIView来控制的,我打算改一下里面的post方法

# utils.py

from rest_framework import status
from rest_framework.response import Response
from rest_framework_jwt.settings import api_settings
from rest_framework_jwt.views import JSONWebTokenAPIView, ObtainJSONWebToken, RefreshJSONWebToken, VerifyJSONWebToken
from datetime import datetime


class MyJSONWebTokenAPIView(JSONWebTokenAPIView):

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            user = serializer.object.get('user') or request.user
            token = serializer.object.get('token')
            response_data = jwt_response_payload_handler(token, user, request)
            response = Response(response_data)
            if api_settings.JWT_AUTH_COOKIE:
                expiration = (datetime.utcnow() +
                              api_settings.JWT_EXPIRATION_DELTA)
                response.set_cookie(api_settings.JWT_AUTH_COOKIE,
                                    token,
                                    expires=expiration,
                                    httponly=True)
            return response
        error_data = jwt_response_payload_error_handler(serializer, request)
        return Response(error_data, status=status.HTTP_200_OK)
      
      


def jwt_response_payload_handler(token, user=None, request=None):
    # 自定义返回 JWT
    return  {
        "msg": "success",
        "status": 200,
        "data": [{
        	# data自定义你接口想返回的信息
            'token': token,
            'username': user.username
        }]
    }

  

def jwt_response_payload_error_handler(serializer, request=None):

    return return {
        "msg": "用户名或者密码错误",
        "status": 400,
        "detail": serializer.errors
    }

      
      
      

class MyObtainJSONWebToken(ObtainJSONWebToken, MyJSONWebTokenAPIView):
    pass


class MyRefreshJSONWebToken(RefreshJSONWebToken, MyJSONWebTokenAPIView):
    pass


class MyVerifyJSONWebToken(VerifyJSONWebToken, MyJSONWebTokenAPIView):
    pass


obtain_jwt_token = MyObtainJSONWebToken.as_view()
refresh_jwt_token = MyRefreshJSONWebToken.as_view()
verify_jwt_token = MyVerifyJSONWebToken.as_view()

# urls.py

from apps.user.restful import utils

urlpatterns = [
    # 获取
    path('api-token-auth/', utils.obtain_jwt_token),

    # 刷新
    path('api-token-refresh/', utils.refresh_jwt_token),

    # 校验
    path('api-token-verify/', utils.verify_jwt_token),
]

这样就避免了修改源码,到时候云上不同步才是大坑

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