django - 令牌授权 Django {"detail":"未提供身份验证凭据。"}
问题描述
我正在尝试在我的服务器 API 上实现基于令牌的授权。但是,当我触发查询时,它会返回{"detail": "Authentication credentials were not provided."}
我已经完成了各种帖子以及 Django 文档中推荐的几乎所有设置。
在我的settings.py
:
INSTALLED_APPS = [
'rest_framework.authtoken',
]
并且,
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
在我的视图文件中:
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
在我在 WSGI 的 Apache Config 文件中
WSGIPassAuthorization On
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP-AUTHORIZATION:%1]
我不知道我是否错过了除此之外的任何东西。令牌是使用命令行在超级用户级别预先生成的。
解决方案
你见过这个相关的 SO 问题吗?
如果您使用的是 mod_wsgi,请忽略 REWRITE 部分,放在,和指令WSGIPassAuthorization On
下方。刚刚让我的 Apache + mod_wsgi 配置工作,看起来像这样:WSGIScriptAlias
WSGIDaemonProcess
WSGIProcessGroup
...
WSGIScriptAlias / /var/www/html/base/wsgi.py
WSGIDaemonProcess django_app python-path=/var/www/html python-home=/var/www/html/env
WSGIProcessGroup django_app
WSGIPassAuthorization On
...
推荐阅读
- reactjs - 替换 Redux 时是否存在 React(状态相关)事件发布子模式?
- python - 具有多个布尔条件或多个函数的函数?
- java - 在多个视图持有者 recyclerview 上投射异常
- javascript - 如何通过 React 类中的动态元素调用原生 JS 函数?
- swift - Swift 嵌套泛型参数推断
- mysql - 如何在存储过程中的事务中向 PHP 发出关于 sql 错误的信号?
- c++ - 为什么我的程序没有接触到我做的功能?
- mysql - 可以在逻辑 ER 图中表示多对一关系吗?
- c++ - 工厂方法说明
- node.js - NodeJs + Promise 包装在一个函数中