课程介绍

认证Authentication

可以在配置文件中配置全局默认的认证方案

  1. REST_FRAMEWORK = {
  2. 'DEFAULT_AUTHENTICATION_CLASSES': (
  3. 'rest_framework.authentication.SessionAuthentication', # session认证
  4. 'rest_framework.authentication.BasicAuthentication', # 基本认证
  5. )
  6. }

也可以在每个视图中通过设置authentication_classess属性来设置

  1. from rest_framework.authentication import SessionAuthentication, BasicAuthentication
  2. from rest_framework.views import APIView
  3. class ExampleView(APIView):
  4. authentication_classes = (SessionAuthentication, BasicAuthentication)
  5. ...

认证失败会有两种可能的返回值:

  • 401 Unauthorized 未认证
  • 403 Permission Denied 权限被禁止

require([“gitbook”], function(gitbook) { var config = {“highlight”:{},”search”:{“maxIndexSize”:1000000},”sharing”:{“facebook”:true,”twitter”:true,”google”:false,”weibo”:false,”instapaper”:false,”vk”:false,”all”:[“facebook”,”google”,”twitter”,”weibo”,”instapaper”]},”fontsettings”:{“theme”:”white”,”family”:”sans”,”size”:2},”livereload”:{}}; gitbook.start(config); });