jwt rest框架返回带有令牌的用户ID [英] jwt rest framework returning user id with token

查看:105
本文介绍了jwt rest框架返回带有令牌的用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jwt-rest-frame 进行用户身份验证,并且我想返回带有令牌的 user_id 。有一些使用 Django-rest-framework <时如何执行此操作的示例/ a>,因此根据此示例,我尝试覆盖 ObtainJSONWenToken jwt视图并执行相同的操作

I'm using jwt-rest-framework for user authentication, and I would like to return a user_id with the token. There are some examples on how to do it when using Django-rest-framework, so according to this example I've tried to override ObtainJSONWenToken jwt view and did the same thing

from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework_jwt.views import ObtainJSONWebToken

    # Create your views here.


    class CustomObtainJSONWebToken(ObtainJSONWebToken):
        """
        Return user id with token

        """
        def post(self, request, *args, **kwargs):
            response = super(CustomObtainJSONWebToken, self).post(request, *args, **kwargs)
            token = Token.objects.get(key=response.data['token'])
            return Response({'token': token.key, 'id': token.user_id})

但是Traceback指出了它自己的令牌

But the Traceback is pointing out to the token it self:

Traceback:  

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  42.             response = get_response(request)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.5/contextlib.py" in inner
  30.                 return func(*args, **kwds)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "/home/copser/.virtualenvs/iToucan-BackHand/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "/home/copser/Documents/iToucan-BackHand/iToucan/itoucan/rest_auth/views.py" in post
  15.         token = Token.objects.get(key=response.data['token'])

问题是用令牌返回 user_id 的正确方法是什么?

The question is what is the proper way of returning user_id with the token, how do I override this view so I can achieve this?

推荐答案

您可以为jwt有效负载响应添加自定义函数吗?

You can add custom function for jwt payload response:

def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'user': user.id
    }

在您的Django设置中添加:

And in your django settings add:

JWT_AUTH = {
    ...
    'JWT_RESPONSE_PAYLOAD_HANDLER': 'your_path_to.jwt_response_payload_handler',
    ...
}

这篇关于jwt rest框架返回带有令牌的用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆