如何在 Route 中添加 django rest framework 身份验证?
How to add a django rest framework authentication in Route?
如何在Route中添加django rest framework认证?
我正在使用 JWT 对我的应用程序进行身份验证。
一切正常。
我需要知道的是如何基于 REST Framework 和 JWT 对特定路由进行身份验证
例子
from rest_framework.permissions import IsAuthenticated
path(r'modulo/app/aula/<modalidade>', IsAuthenticated AppAulaAdd.as_view(), name='app_aula')
或
from rest_framework.decorators import authentication_classes
path(r'modulo/app/aula/<modalidade>', authentication_classes(AppAulaAdd.as_view()), name='app_aula')
两者都不行。
您在问题中混淆了概念。权限 类 根据用户在系统或会话中的状态(即 IsAuthenticated、IsStaff 等)控制对资源的访问,而身份验证 类 控制对用户进行身份验证的方法,例如BasicAuthentication 或您的情况下的 JSONWebTokenAuthentication。此外,您应该直接在视图中添加两种类型的 类,这是更好的做法(来自 https://www.django-rest-framework.org/api-guide/authentication/):
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
但是如果出于某种原因 100% 需要在您的 urls 文件(路由)中添加权限,您可以执行以下操作:
from rest_framework.decorators import permission_classes
from rest_framework.permissions import IsAuthenticated
path(r'modulo/app/aula/<modalidade>', (permission_classes([IsAuthenticated])(AppAulaAdd)).as_view(), name='app_aula')
希望对您有所帮助。
如何在Route中添加django rest framework认证?
我正在使用 JWT 对我的应用程序进行身份验证。 一切正常。
我需要知道的是如何基于 REST Framework 和 JWT 对特定路由进行身份验证
例子
from rest_framework.permissions import IsAuthenticated
path(r'modulo/app/aula/<modalidade>', IsAuthenticated AppAulaAdd.as_view(), name='app_aula')
或
from rest_framework.decorators import authentication_classes
path(r'modulo/app/aula/<modalidade>', authentication_classes(AppAulaAdd.as_view()), name='app_aula')
两者都不行。
您在问题中混淆了概念。权限 类 根据用户在系统或会话中的状态(即 IsAuthenticated、IsStaff 等)控制对资源的访问,而身份验证 类 控制对用户进行身份验证的方法,例如BasicAuthentication 或您的情况下的 JSONWebTokenAuthentication。此外,您应该直接在视图中添加两种类型的 类,这是更好的做法(来自 https://www.django-rest-framework.org/api-guide/authentication/):
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
但是如果出于某种原因 100% 需要在您的 urls 文件(路由)中添加权限,您可以执行以下操作:
from rest_framework.decorators import permission_classes
from rest_framework.permissions import IsAuthenticated
path(r'modulo/app/aula/<modalidade>', (permission_classes([IsAuthenticated])(AppAulaAdd)).as_view(), name='app_aula')
希望对您有所帮助。