Django Elastic Beanstalk令牌身份验证

2024-06-01 01:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在开发一个应用程序,它使用Django Rest框架中的令牌身份验证连接到Django服务器,在本地它工作得很好,身份验证没有问题,我甚至能够对经过身份验证的用户使用可视可浏览api。现在,当我将项目部署到Elastic Beanstalk时,我不断得到Unauthorized,同时使用所有需要身份验证的URL。 我试过herehere中的答案。似乎都不起作用,我使用this document进行http重定向,如aws文档所示。你知道吗


以下是my Django视图中的身份验证类:

authentication_classes = (BasicAuthentication,TokenAuthentication,)

以及权限类:

permission_classes = (gbets_permissions.IsSameUser,permissions.IsAuthenticated)

我已经仔细检查了settings.py,并在本地环境中重试了。你知道吗

谢谢你的帮助,我不习惯在后端工作,我现在被卡住了,谢谢。如果你需要更多的信息,请告诉我。你知道吗


编辑1:

在网上找到了更多的答案,并再次查看了文档Using the AWS Elastic Beanstalk Python Platform和教程here,因此我将.ebextensions中的文件更改为:

packages:
  yum:
    libmemcached-devel: '0.31'

container_commands:
  01_syncdb:
    command: "django-admin.py syncdb --noinput"
    leader_only: true
  02_migrate:
    command: "django-admin.py migrate --noinput"
    leader_only: true
  03_collectstatic:
    command: "django-admin.py collectstatic --noinput"
  04_wsgipass:
    command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: krimis_web.settings
  aws:elasticbeanstalk:container:python:
    WSGIPath: krimis_web/wsgi.py

commands:
  WSGIPassAuthorization:
    command: sed -i.bak '/WSGIScriptAlias/ a WSGIPassAuthorization On' config.py
    cwd: /opt/elasticbeanstalk/hooks


编辑2:

我无法使用在我找到的任何答案中找到的任何文件、命令或配置来更改addWSGIPassAuthorization On,但是我可以通过手动编辑wigs.conf内部的etc/httpd/conf.d/wigs.conf来解决问题,方法是在VirtualHost DOM之间放置以下内容:

WSGIPassAuthorization On

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

有了这个Auth头,Django就可以接收到Auth头,身份验证似乎也可以工作。你知道吗

我还没有检查部署后是否需要再次手动更改此部分

我会不断更新的。你知道吗


Tags: django答案py身份验证aws编辑heresettings