Django的JWT机制工作流程

https://blog.csdn.net/bin_1022/article/details/81278513

 

django-rest-framework-jwt token 怎么解码得到用户名?

    def get(self, request):

        auth = request.auth  # 前段传来的token在request.auth中
        print(auth)
        from rest_framework_jwt.utils import jwt_decode_handler
        b = jwt_decode_handler(auth)
        print(b)
        return Response('11')

解码后:

{'username': '18120192554', 'exp': 1536545855, 'user_id': 2, 'email': None}

如何手动签发token?

例如,注册成功后,返回前段token

    def post(self, request):
        serializer = RegisterSerializer(data=request.data)
        if serializer.is_valid():
            user_o = serializer.save()
            # 注册成功后,手动签发token
            payload = jwt_payload_handler(user_o)
            token = jwt_encode_handler(payload)
            return Response({'status': 0, 'meg': '创建成功', 'token': token})
        else:
            return Response(serializer.errors)

 

posted @ 2018-09-09 10:09  20180616  阅读(157)  评论(0编辑  收藏  举报