【问题标题】:Django oauth toolkit not work with apacheDjango oauth 工具包不适用于 apache
【发布时间】:2016-02-12 09:28:40
【问题描述】:

我使用django restframework & django-oauth-toolkit
我在localhost 上测试我的代码,api 运行良好

这是响应头:

Allow → GET, POST, HEAD, OPTIONS
Content-Type → application/json
Date → Wed, 11 Nov 2015 09:41:50 GMT
Server → WSGIServer/0.1 Python/2.7.10
Vary → Accept, Authorization, Cookie
X-Frame-Options → SAMEORIGIN

但是我用 apache 把项目放在虚拟机上
oauth 不起作用。我使用了正确的令牌,但得到了401 UNAUTHORIZED

{
  "detail": "Authentication credentials were not provided."
}

这是响应头:

Allow → GET, POST, HEAD, OPTIONS
Connection → Keep-Alive
Content-Length → 58
Content-Type → application/json
Date → Wed, 11 Nov 2015 09:40:37 GMT
Keep-Alive → timeout=5, max=100
Server → Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5
Vary → Accept,Authorization
WWW-Authenticate → Bearer realm="api"
X-Frame-Options → SAMEORIGIN

为什么会这样??? 请指导我

settings.py

OAUTH2_PROVIDER = {
    'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'},
}


AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'oauth2_provider.backends.OAuth2Backend',
)


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    )
}

【问题讨论】:

    标签: django oauth django-rest-framework


    【解决方案1】:

    如果您使用的是 mod_wsgi,您可能需要在某处添加:

    WSGIPassAuthorization On
    

    在 AWS elasticbeanstalk 中,修补 apache 配置的一种肮脏方式是将此命令添加到您的 .ebextensions/your-environemnt.conf

    commands:
      00_WSGIPassAuthorization:
        command: sed -i.bak '/WSGIScriptAlias/ aWSGIPassAuthorization On' config.py
        cwd: /opt/elasticbeanstalk/hooks
    

    很脏,因为每次重新加载服务器时它都会添加一行。

    相关Authorization Credentials Stripped --- django, elastic beanstalk, oauth

    【讨论】:

    • AWS 的命令是创建一个覆盖 apache 设置的文件。需要时不时检查一下,以防亚马逊 linux 镜像改变了钩子的位置
    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 2020-06-26
    • 2015-01-19
    • 2015-10-19
    • 2017-04-01
    • 2014-04-02
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多