Django-rest-framework api 添加 SessionAuthentication 作为可选

Django-rest-framework api add SessionAuthentication as optional

您好,我目前有我的 api 使用此 simple-JWT 包进行 jwt 令牌身份验证,效果很好。但是现在当我尝试使用 Ajax 从 django 网站应用程序调用 api 时,该应用程序来自已经登录的页面用户,但它仍然需要我使用 jwt access_token。

我的 ajax 来自已登录页面用户的调用:

$.ajax({
       type: "POST",
       url: "/api/add_favorite/" + property_id + "/",
       beforeSend: function (xhr) {
              xhr.setRequestHeader('Authorization', 'Bearer {{ refresh_token }}');
       },
       success: function (data) {
       if (data.code == 200) {
              alert('added to favorite');
              replace_part_1 = '<a id="mylink2" href="#" value="' + property_id +'"><i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite"></i></a>'
              $("a[value='" + property_id + "']").replaceWith(replace_part_1);
             }
       }
});

现在我不想设置 header 授权,因为在页面中用户已经登录,所以 session 已经设置。

所以我尝试将 Django Session 身份验证添加到 api,如下所示:

@api_view(['POST'])
@authentication_classes([SessionAuthentication, JWTAuthentication])
@permission_classes([IsAuthenticated])
def add_favorite(request, property_id):
    if request.method == 'POST':
        try:
            favorite_property = Property.objects.get(pk=property_id)
            if request.user.is_authenticated:
                login_user = request.user
                if not login_user.properties.filter(pk=property_id).exists():
                    login_user.properties.add(favorite_property)

                    return JsonResponse({'code':'200','data': favorite_property.id}, status=200)
                else:
                    return JsonResponse({'code':'404','errors': "Property already exists in favorite"}, status=404)

        except Property.DoesNotExist:
            return JsonResponse({'code':'404','errors': "Property not found"}, status=404)

删除 header 后我的 Ajax :

$.ajax({
       type: "POST",
       url: "/api/add_favorite/" + property_id + "/",
       },
       success: function (data) {
       if (data.code == 200) {
              alert('added to favorite');
              replace_part_1 = '<a id="mylink2" href="#" value="' + property_id +'"><i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite"></i></a>'
              $("a[value='" + property_id + "']").replaceWith(replace_part_1);
             }
       }
});

我从 Ajax 调用中删除了集合 header 现在我得到 403 return 代码:

Failed to load resource: the server responded with a status of 403 (Forbidden)

我的设置:

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    # 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}

我不知道为什么 session 身份验证不起作用,因为 Ajax 调用来自已登录的页面用户。

感谢阅读!

因为您要在 ajax 请求中添加 Authentication header,如果请求 [=21] 中存在 Authentication,Django 会自动使用 TokenAuthentication =].删除它以使用 SessionAuthentication。

当您切换到使用 SessionAuthentication 时可能会出现问题,如果没有 CSRF 令牌,Django 将拒绝您的 unsafe 请求,更多详细信息 here