如何使用访问和刷新令牌返回自定义数据以识别 Django Rest Framework 简单 JWT 中的用户?

dee*_*pto 9 django-rest-framework django-rest-framework-jwt

在 Django 中,超级用户可以根据他们的滚动添加更多用户。我正在使用带有 DRF 的简单 JWT 进行身份验证。但仅通过访问和刷新令牌无法检测用户类型。

这是我的 settings.py 文件

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
    'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',),


}
Run Code Online (Sandbox Code Playgroud)

网址.py

from django.contrib import admin
from django.urls import path, include
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView


urlpatterns = [

    path('admin/', admin.site.urls),
    path('', include('Manage_Merchants.urls')),

    path('api-auth', include('rest_framework.urls')),
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),


]
Run Code Online (Sandbox Code Playgroud)

当我通过 Postman 访问 127.0.0.1:8000/api/token/ 时,它要求输入用户名和密码。当我输入用户名和密码时,它会生成一个刷新和访问令牌。 使用 Postman 生成带有 DRF 的 JWT

那么如何识别为超级用户或其他用户创建的超级用户生成的令牌?如何将更多值作为字典与访问和刷新令牌一起传递以识别用户类型?

小智 15

在版本中,djangorestframework-simplejwt==4.4.0它是方法validate而不是to_representation,意思是:

在您serializer.py需要覆盖TokenObtainPairSerializer以包含您要在响应中发送的所有数据

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        # The default result (access/refresh tokens)
        data = super(CustomTokenObtainPairSerializer, self).validate(attrs)
        # Custom data you want to include
        data.update({'user': self.user.username})
        data.update({'id': self.user.id})
        # and everything else you want to send in the response
        return data
Run Code Online (Sandbox Code Playgroud)

现在,您views.py需要覆盖 TokenObtainPairView 以将其与新的序列化程序配对。

from .serializers import CustomTokenObtainPairSerializer


class CustomTokenObtainPairView(TokenObtainPairView):
    # Replace the serializer with your custom
    serializer_class = CustomTokenObtainPairSerializer
Run Code Online (Sandbox Code Playgroud)

现在将它映射到您的 url.py

from rest_framework_simplejwt.views import TokenRefreshView, TokenVerifyView
from . import views

urlpatterns = [
    # This one now has the custom view mapped with the custom serializer that includes the desired data
    path('token/', views.CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('token/verify/', TokenVerifyView.as_view(), name='token_verify')
]

Run Code Online (Sandbox Code Playgroud)


Rez*_*adi 8

就像库马尔所说,你应该覆盖 TokenObtainPairView。让我深入了解一下:

在您的核心应用程序 views.py 中创建一个新的 classView,或者如果您想要更简洁的代码,您可以创建一个名为 jwt_token_patched 的新应用程序,并在其中创建一个 views.py 文件。现在添加以下代码:

class TokenObtainPairPatchedView(TokenObtainPairView):
    """
    Takes a set of user credentials and returns an access and refresh JSON web
    token pair to prove the authentication of those credentials.
    """
    serializer_class = serializers.TokenObtainPairPatchedSerializer

    token_obtain_pair = TokenObtainPairView.as_view()
Run Code Online (Sandbox Code Playgroud)

现在为序列化程序添加以下内容:

class TokenObtainPairPatchedSerializer(TokenObtainPairSerializer):
     def to_representation(self, instance):
         r = super(TokenObtainPairPatchedSerializer, self).to_representation(instance)
         r.update({'user': self.user.username})
         return r
Run Code Online (Sandbox Code Playgroud)

当序列化程序以 json 格式返回数据时调用 to_representation() 方法,因此您可以在其中添加任何您想要的内容。记住我只是将用户名放在用户字段值中,您可以在其中添加您想要的用户的任何项目值。

还为此创建一个 url,从现在开始使用该方法获取令牌。如果您愿意,请随时提出任何问题。希望它有用:)