未调用自定义 rest_framework 身份验证

Custom rest_framework authentication not getting called

我正在使用 djangorestframework 3.9.2

我对 rest_framework

进行了以下设置
....
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'my_project.middlewares.authentication.CentralAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'my_project.middlewares.authorization.CentralAuthorization'
    ],
}
....

我的目录结构是这样的

├── app1/
└── my_project
    ├── __init__.py
    ├── middlewares
    │   ├── __init__.py
    │   ├── authentication.py
    │   └── authorization.py
    ├── settings
    │   ├── __init__.py
    │   ├── commons.py
    │   ├── development.py
    │   └── logger.py
    ├── urls.py
└── wsgi.py

每当我访问 URL.

时,我的身份验证脚本都不会被调用

这里有什么我遗漏的吗?

这些权限 classes 或身份验证 classes 不是中间件,它不会被执行自动地。这些 classes 仅适用于 DRF 视图

因此,定义一个 DRF 视图并在您的 urls.py 模块中连接该视图

from rest_framework.views import APIView
from rest_framework.response import Response


class FooAPI(APIView):
    def get(self, request, *args, **kwargs):
        return Response({'message': 'success'})


urlpatterns = [
    path('foo/', FooAPI.as_view(), name='foo-api'),

]

在这个FooAPIclass的执行过程中,DRF会调用默认的permission_classesauthentication_classes