Rest Framework 视图外的 Django Rest Framework 权限
Django Rest Framework permissions outside Rest Framework view
我正在使用 Rest Framework Token 身份验证。这意味着我不知道用户是否在休息框架视图之外进行了身份验证,例如:(常规 django 视图)。 Rest Framework 令牌身份验证是一个自定义身份验证系统,只能在 rest 框架视图中使用。
在正常的休息框架视图中,我可以使用以下方法限制经过身份验证的用户的端点:
class ExampleView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
但是对于常规的 Django 视图,我将如何做到这一点。例如:
def someDjangoView(request):
'''
Note that I cannout use request.user.is_authenticated.
It will always return false as I am using rest framework token authentication.
Which means the request parameter should be of rest framework's and not django's built-in.
'''
content = {"detail": "Only authenticated users should access this"}
return JsonResponse(content)
我陷入了一种情况,我必须知道用户是否在休息框架视图之外进行了身份验证(自定义身份验证)。
有什么办法吗?
您可以在基于函数的视图中使用 api_view
装饰器来启用 DRF:
from rest_framework.decorators import api_view, authentication_classes
@api_view(http_method_names=['GET', 'POST'])
@authentication_classes([YourTokenAuthenticationClass])
def someDjangoView(request):
print(request.user)
...
return JsonResponse(content)
DRF 建立在内置 Django contrib.auth
用户身份验证系统之上。因此,对于常规的 Django 视图,您可以使用 regular methods provided by contrib.auth
.
DRF 还支持基于会话的身份验证(通常是使用 contrib.auth
时的默认设置)。这是理想的,例如,当您在浏览器中有一些 JavaScript 代码 运行 与用户会话时。
Note that I cannout use request.user.is_authenticated.
It will always return false as I am using rest framework token authentication
如果您正在使用 rest framework 令牌身份验证,那么您必须使用与之兼容的视图。 request.user.is_authenticated
是 django 内置的 contrib.auth
系统的一部分。但是,您必须对用户进行身份验证才能使其为真。 Rest Framework 会为你做这件事。如果你没有使用 rest 框架,你必须自己授权用户!
一个简单的答案可能是装饰您的视图,使它们利用您定义的其余框架身份验证:
@api_view(['GET'])
@authentication_classes(...) # if defaults are not applied
@permission_classes(...) # to apply permissions you need
def view(request):
return Response({"message": "Hello for today! See you tomorrow!"})
我正在使用 Rest Framework Token 身份验证。这意味着我不知道用户是否在休息框架视图之外进行了身份验证,例如:(常规 django 视图)。 Rest Framework 令牌身份验证是一个自定义身份验证系统,只能在 rest 框架视图中使用。
在正常的休息框架视图中,我可以使用以下方法限制经过身份验证的用户的端点:
class ExampleView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
但是对于常规的 Django 视图,我将如何做到这一点。例如:
def someDjangoView(request):
'''
Note that I cannout use request.user.is_authenticated.
It will always return false as I am using rest framework token authentication.
Which means the request parameter should be of rest framework's and not django's built-in.
'''
content = {"detail": "Only authenticated users should access this"}
return JsonResponse(content)
我陷入了一种情况,我必须知道用户是否在休息框架视图之外进行了身份验证(自定义身份验证)。
有什么办法吗?
您可以在基于函数的视图中使用 api_view
装饰器来启用 DRF:
from rest_framework.decorators import api_view, authentication_classes
@api_view(http_method_names=['GET', 'POST'])
@authentication_classes([YourTokenAuthenticationClass])
def someDjangoView(request):
print(request.user)
...
return JsonResponse(content)
DRF 建立在内置 Django contrib.auth
用户身份验证系统之上。因此,对于常规的 Django 视图,您可以使用 regular methods provided by contrib.auth
.
DRF 还支持基于会话的身份验证(通常是使用 contrib.auth
时的默认设置)。这是理想的,例如,当您在浏览器中有一些 JavaScript 代码 运行 与用户会话时。
Note that I cannout use request.user.is_authenticated. It will always return false as I am using rest framework token authentication
如果您正在使用 rest framework 令牌身份验证,那么您必须使用与之兼容的视图。 request.user.is_authenticated
是 django 内置的 contrib.auth
系统的一部分。但是,您必须对用户进行身份验证才能使其为真。 Rest Framework 会为你做这件事。如果你没有使用 rest 框架,你必须自己授权用户!
一个简单的答案可能是装饰您的视图,使它们利用您定义的其余框架身份验证:
@api_view(['GET'])
@authentication_classes(...) # if defaults are not applied
@permission_classes(...) # to apply permissions you need
def view(request):
return Response({"message": "Hello for today! See you tomorrow!"})