认证Authentication
认证Authentication
REST_FRAMEWORK = {
\'DEFAULT_AUTHENTICATION_CLASSES\': (
\'rest_framework.authentication.BasicAuthentication\', # 基本认证
\'rest_framework.authentication.SessionAuthentication\', # session认证
)
}
也可以在每个视图中通过设置authentication_classess属性来设置
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.views import APIView
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
...
认证失败会有两种可能的返回值:
-
401 Unauthorized 未认证
-
403 Permission Denied 权限被禁止
版权声明:本文为HZLS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。