本文介绍了在django-rest-framework-jwt中存储更多的默认信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.8版和带有django-rest-framework-jwt的身份验证.验证后,我们的应用程序将返回到前端,并提供以下信息:

I am using Django version 1.8 and authentication with django-rest-framework-jwt.After authentication, our application will return to front-end with information:

from rest_framework_jwt.settings import api_settings
from rest_framework.response import Response
from django.contrib.auth.models import User


jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

user.profile_picture = "test_profile_picture.jpg" #user is django.contrib.auth.models.User object
payload = jwt_payload_handler(user) 
token = jwt_encode_handler(payload)

 jwt_login_response = {
        "token": token
 }

return Response(jwt_login_response,status=status.HTTP_200_OK)

在前端,我们将通过

import jwtDecode from 'jwt-decode';
var decodedToken = jwtDecode(token);

但是目前,decodedToken仅包含django-rest-framework-jwt似乎默认的值

But at the moment decodedToken only contains value which seem default by django-rest-framework-jwt

Object {username: "test", user_id: 9, email: "[email protected]", exp: 1454754137}

我想在jwt中存储更多信息,例如profile_picture,但我不知道如何.我真的很想找到有关此问题的解决方案.

I want to store more information in jwt like profile_picture but I don't know how. I really want to find a solution about this problem.

推荐答案

您可以使用 JWT_PAYLOAD_HANDLER设置.

You can override the payload that is attached to the token with the JWT_PAYLOAD_HANDLER setting.

您已经注意到,默认实现包括usernameuser_idemailexp(到期时间).您可以找到默认实现上GitHub .

The default implementation includes the username, user_id, email, and exp (expiration time), as you've noticed. You can find the default implementation on GitHub.

这篇关于在django-rest-framework-jwt中存储更多的默认信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:22