Flask无法读取ElasticBeanstalk上的Authorization标头

bur*_*gar 5 wsgi amazon-web-services python-3.x flask-peewee amazon-elastic-beanstalk

编辑:进一步阅读两个解决方案。

我已将Flask应用程序部署到AWS ElasticBeanstalk。应用无法读取请求中的“ 授权 ”标头。

错误日志报告:

KeyError: 'HTTP_AUTHORIZATION'
Run Code Online (Sandbox Code Playgroud)

错误追踪至:

@application.before_request
    def before_request():
       try:
          token = request.headers['Authorization'].split(' ')[-1]
          user = User.get(token=token)
          g.user = user
       except ValueError as e:
        abort(401)
Run Code Online (Sandbox Code Playgroud)

应用程序目录:

app/
    .elasticbeanstalk
    application.py
    virt
    .ebignore
    requirements.txt
Run Code Online (Sandbox Code Playgroud)

环境配置将WSGIPath设置为application.py

aws:elasticbeanstalk:container:python:
 NumProcesses: '1'
 NumThreads: '15'
 StaticFiles: /static/=static/
 WSGIPath: application.py
Run Code Online (Sandbox Code Playgroud)

环境运行Python 3.6和以下组件:

Click==7.0
Flask==1.0.2
Flask-RESTful==0.3.7
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.1
peewee==3.9.2
psycopg2==2.7.7
python-dotenv==0.10.1
pytz==2018.9
six==1.12.0
Werkzeug==0.14.1
Run Code Online (Sandbox Code Playgroud)

还有其他要求吗?

尝试的(失败的)解决方案:

我为此花了很多时间,并尝试配置WSGIPassAuthorization,(根据此处和其他地方的建议),但是,我没有成功。

应用程序目录包含解决方法:

app/
    .elasticbeanstalk
    .ebextensions/
        wsgi_custom.config
    application.py
    virt
    .ebignore
    requirements.txt
Run Code Online (Sandbox Code Playgroud)

当我尝试创建包含.ebextensions / wsgi_custom.config的eb环境时,EB CLI报告一个错误,指出YAML无效:

    ERROR: InvalidParameterValueError - The configuration file .ebextensions/wsgi_custom.config in application version app-190310_100513 contains invalid YAML or JSON. YAML exception: Invalid Yaml: while scanning a simple key
 in "<reader>", line 7, column 5:
        WSGIPassAuthorization On
        ^
could not found expected ':'
 in "<reader>", line 7, column 29:
    On
      ^
, JSON exception: Invalid JSON: Unexpected character (f) at position 0.. Update the configuration file.
Run Code Online (Sandbox Code Playgroud)

内容.ebextensions / wsgi_custom.config

    files:
       "/etc/httpd/conf.d/wsgi_custom.conf":
         mode: "000644"
         owner: root
         group: root
         content: |
           WSGIPassAuthorization On
Run Code Online (Sandbox Code Playgroud)

我的YAML验证工具报告有效的YAML。

注:根据AWS YAML建议,将编辑器设置为使用空格。

编辑:解决方案1

解决了以上示例中的YAML验证错误。我相信验证错误是一个红鲱鱼。脚本中提到的.conf文件现在具有有效名称。

.ebextensions / wsgi_custom.config的内容:

    files:
  "/etc/httpd/conf.d/wsgihacks.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      WSGIPassAuthorization On
Run Code Online (Sandbox Code Playgroud)

编辑:使用container_commands的解决方案2

使用container_commandsElasticBeanstalk上设置WSGIPassAuthorization

步骤1.创建.ebextensions / wsgi_custom.config

app/
    .elasticbeanstalk
    .ebextensions/
        wsgi_custom.config
    application.py
    virt
    .ebignore
    requirements.txt
Run Code Online (Sandbox Code Playgroud)

wsgi_custom.config

container_commands:
    01wsgipass:
        command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
Run Code Online (Sandbox Code Playgroud)

步骤2. 重新启动 EB环境。

Flask应用现在可以在请求中读取“ Authorization ”标题。

万岁:)

如果有人可以指出我对WSGI的清晰讨论,将不胜感激。

jon*_*rpe 0

使用container_commands在 ElasticBeanstalk 上设置WSGIPassAuthorization

步骤 1. 创建.ebextensions/wsgi_custom.config

app/
    .elasticbeanstalk
    .ebextensions/
        wsgi_custom.config
    application.py
    virt
    .ebignore
    requirements.txt
Run Code Online (Sandbox Code Playgroud)

wsgi_custom.config

container_commands:
    01wsgipass:
        command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
Run Code Online (Sandbox Code Playgroud)

步骤 2.重新启动EB 环境。

Flask 应用程序现在可以读取请求中的“授权”标头。