rest framework-jwt 我如何处理有效载荷?
restframework-jwt how can I handle payload?
我使用 Django restframework 来实现 api 服务器。
我还使用 djangorestframework-jwt 进行令牌认证。
[urls.py]
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import refresh_jwt_token
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('rest_auth.urls')),
path('registration/', include('rest_auth.registration.urls')),
path('refresh-token/', refresh_jwt_token),
]
一切正常。但我想知道如何从令牌中提取有效负载?
例如,有文章table。
[/article/serializers.py]
from rest_framework import serializers
from . import models
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = models.Article
fields = '__all__'
[models.py]
from django.db import models
class Article(models.Model):
subject = models.CharField(max_length=20)
content = models.CharField(max_length=100)
[views.py]
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from . import models, serializers
class Article(APIView):
def get(self, request, format=None):
all_article = models.Article.objects.all()
serializer = serializers.ArticleSerializer(all_article, many=True)
return Response(data=serializer.data, status=status.HTTP_200_OK)
在这种情况下,我想 return 仅正确响应负载 ['userid'] == 文章的用户 ID。
如何从 jwt 令牌中提取用户名?
之前我只是用jwt,没有用djangorestframework-jwt,所以直接解码请求数据就可以用了
但是现在我用的是djangorestframework-jwt,我很迷茫怎么办。
有什么解决办法吗?
谢谢。
我在此处查看库的文档后解决了这个问题
https://jpadilla.github.io/django-rest-framework-jwt/
我需要根据我的应用程序的要求将所有功能覆盖为 return 自定义响应。
我们还需要在我的应用程序的 settings.py 中的 JWT_AUTH 中指定任何其他设置。
即
JWT_AUTH = {
'JWT_RESPONSE_PAYLOAD_HANDLER':
'myapp.utils.jwt_response_payload_handler',
}
并且需要在我的应用程序的 util 文件夹中定义 jwt_response_payload_handler
函数以覆盖默认函数。
我使用 Django restframework 来实现 api 服务器。
我还使用 djangorestframework-jwt 进行令牌认证。
[urls.py]
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import refresh_jwt_token
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('rest_auth.urls')),
path('registration/', include('rest_auth.registration.urls')),
path('refresh-token/', refresh_jwt_token),
]
一切正常。但我想知道如何从令牌中提取有效负载?
例如,有文章table。
[/article/serializers.py]
from rest_framework import serializers
from . import models
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = models.Article
fields = '__all__'
[models.py]
from django.db import models
class Article(models.Model):
subject = models.CharField(max_length=20)
content = models.CharField(max_length=100)
[views.py]
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from . import models, serializers
class Article(APIView):
def get(self, request, format=None):
all_article = models.Article.objects.all()
serializer = serializers.ArticleSerializer(all_article, many=True)
return Response(data=serializer.data, status=status.HTTP_200_OK)
在这种情况下,我想 return 仅正确响应负载 ['userid'] == 文章的用户 ID。
如何从 jwt 令牌中提取用户名?
之前我只是用jwt,没有用djangorestframework-jwt,所以直接解码请求数据就可以用了
但是现在我用的是djangorestframework-jwt,我很迷茫怎么办。
有什么解决办法吗?
谢谢。
我在此处查看库的文档后解决了这个问题
https://jpadilla.github.io/django-rest-framework-jwt/
我需要根据我的应用程序的要求将所有功能覆盖为 return 自定义响应。
我们还需要在我的应用程序的 settings.py 中的 JWT_AUTH 中指定任何其他设置。
即
JWT_AUTH = {
'JWT_RESPONSE_PAYLOAD_HANDLER':
'myapp.utils.jwt_response_payload_handler',
}
并且需要在我的应用程序的 util 文件夹中定义 jwt_response_payload_handler
函数以覆盖默认函数。