设置PERMISSION_CLASSES和AUTHENTICATION_CLASSES
可以在setting中全局设置进行
1 2 3 4 5 6 7 8 9 10 11 12 |
REST_FRAMEWORK = { # 权限认证 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), # 身份验证 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } |
也可以在指定的ViewSet下设置权限,比如对商品信息做权限认证,需登录后才能获得信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class GoodsViewSet(viewsets.ReadOnlyModelViewSet): """ 商品列表 商品详情 """ queryset = Goods.objects.all() # 序列化 serializer_class = GoodsSerializer # 分页 pagination_class = StandardResultsSetPagination # 过滤 filter_backends = (filters.DjangoFilterBackend, SearchFilter, OrderingFilter) filterset_class = GoodsFilter ordering_fields = ('add_time', ) search_fields = ('name', 'goods_desc') #Token认证 permission_classes = (IsAuthenticated,) |
另一篇参考: