在带有JWT令牌的Django Rest Framework中对POST请求的未经授权的响应

问题描述 投票:0回答:1

我正在使用Django Rest Framework构建REST API。我目前遇到一个问题,其中某些端点返回HTTP 401未经授权,而绝大多数端点返回正确的响应。为了进行身份验证,我将JWT令牌与djangorestframework-simplejwt一起使用。

我已将Django配置为对djangorestframework-simplejwt使用令牌身份验证。

# rest framework config settings
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
        # 'rest_framework.permissions.AllowAny',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],

当我在请求中传递有效的访问令牌时,绝大多数端点都会返回有效的数据。如果我没有发送有效的令牌,则会收到HTTP 403。

另一方面,我有一些自定义API视图,无论是否传递有效的令牌,这些视图都会返回HTTP401。>

我已将代码包含在下面的问题视图之一中。

class CheckDifferentialView(generics.GenericAPIView):
    permission_classes = [IsAuthenticated]
    authentication_classes = [TokenAuthentication]
    serializer_class = QuizDifferentialSerializer

    def post(self, request, *args, **kwargs):
        """
            A function to check a quiz question and to update a user's record of questions answered
        """
        print(request.META)
        if 'answer' not in request.data:
            return JsonResponse({'Error': 'answer not found in request'}, status=status.HTTP_400_BAD_REQUEST)

        answer = get_object_or_404(Differential, pk=request.data['answer'])
        serializer = QuizDifferentialSerializer(answer)

        if answer.is_correct:
            pass
            # record correct results
        else:
            pass
            # record failed result

        return Response(serializer.data, status=status.HTTP_200_OK)

这是我用来测试API的脚本

import requests
import json

POST_LOGIN_URL = 'http://localhost:8000/api/v1/token/'
POST_URL= 'http://localhost:8000/api/v1/check_differential'
REQUEST_URL = 'http://localhost:8000/api/v1/users'

with requests.Session() as session:
    post = session.post(POST_LOGIN_URL, json={"username": "j", "monkey": "aphextwin21"})
    token = json.loads(post.text)['access']
    headers = {'Authorization': 'Bearer ' + token}

    r = session.post(POST_URL, headers=headers, json={"answer": "2"})
    # r = session.get(REQUEST_URL, headers=headers)
    print(token)

print(r.text, r.status_code)

期望的行为是,如果我向该端点发送带有有效令牌的POST请求,这将授权我并继续执行该请求。如果没有给出带有有效访问令牌的Authorization标头,那么我希望它拒绝该请求。

我正在使用Django Rest Framework构建REST API。我目前遇到一个问题,其中某些端点返回未经授权的HTTP 401,而绝大多数端点返回正确的...

python django django-rest-framework django-rest-framework-jwt django-rest-framework-simplejwt
1个回答
0
投票

视图的身份验证类仅显式设置为TokenAuthentication。它不适用于JWT令牌。

© www.soinside.com 2019 - 2024. All rights reserved.