我刚刚在一个Django项目中安装了django-rest-framework-simplejwt,如果我发送了一个无效的Authentication Bearer <<token>>头,它会用401code阻塞我的请求。这是预期的行为。问题是,如果我删除了Authentication头,我就可以访问我的视图,所以它是不受保护的。
如果没有Authentication头,如何保护视图返回401?
以下是视图代码:
class AuthTestView(views.APIView):
http_method_names = ['get']
def get(self, request):
response_data = {
'result': 'Restricted access test ok.',
}
return Response(data=response_data, status=status.HTTP_200_OK)发布于 2019-10-09 11:36:31
尝试:
from rest_framework.permissions import IsAuthenticated
class AuthTestView(views.APIView):
http_method_names = ['get']
permission_classes=[IsAuthenticated]
def get(self, request):
response_data = {
'result': 'Restricted access test ok.',
}
return Response(data=response_datahttps://stackoverflow.com/questions/58296438
复制相似问题