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('')
解码后:
{'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)