2

对于我当前的系统,我使用Simple-JWT作为我的用户身份验证。并且还使用Django REST Framework API Key。我对 Simple-JWT 的简单性感到满意。但是,我想添加一个权限,它需要我的 Api-Key 才能查看令牌页面。

至于现在,如果我想获得一个 JWT Token,

我可以简单地转到 /project/api/token/ (获取访问和刷新令牌)

或者

/project/api/refresh/(刷新访问令牌)

在我的 settings.py 文件中,我设置了 DEFAULT_AUTHENTICATION_CLASSES 和 DEFAULT_PERMISSION_CLASSES。据我了解,如果我将“HasAPIKey”作为默认权限类,则所有页面都需要 Api-Key。

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework_simplejwt.authentication.JWTAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework_api_key.permissions.HasAPIKey',
),
}

然而 /token/ 和 /refresh/ 仍然可以在没有 Api-Key 的情况下访问。因此,我的目标是确保这些 URL 需要我的 Api-Key。

(注:我对“simple-jwt”提供令牌的方式非常满意。它很容易实现。我只是想添加“HasAPIKey”权限)

4

1 回答 1

0

Create a new view and inherit the views from rest_framework_simplejwt. Create functions for both TokenObtainPairView and TokenRefreshView. Insert those two views into the parameter for the custom view. Only then insert the permission class. As of now I want my custom views to only be accessed with the valid API Key.

views.py

from rest_framework_simplejwt.views import(
TokenObtainPairView,
TokenRefreshView,
)

class NewTokenObtainPairView(TokenObtainPairView):
     permission_classes = (HasAPIKey,)

class NewTokenRefreshView(TokenRefreshView):
     permission_classes = (HasAPIKey,)
于 2020-09-25T13:09:39.353 回答