全局配置的认证方案:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication', # 基本认证
'rest_framework.authentication.SessionAuthentication', # session认证
)
}
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
class BookInfoViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin,GenericViewSet):
"""使用GenericViewSet实现返回列表和单一值"""
authentication_classes = (SessionAuthentication, BasicAuthentication)
认证失败会有两种可能的返回值:
- 401 Unauthorized 未认证
- 403 Permission Denied 权限被禁止