gpt4 book ai didi

nginx - 如何配置 nginx 将用户信息传递到 wsgi/flask

转载 作者:行者123 更新时间:2023-12-02 05:02:35 24 4
gpt4 key购买 nike

我有一个 nginx http 服务器,它对用户进行身份验证,并通过 wsgi 将经过身份验证的请求传递到 Flask 应用程序。当我从 flask 应用程序打印整个标题时,没有可用的用户信息。

是否可以让 nginx 在请求 header 中包含用户名?

这是带有身份验证配置的服务器 block ...

server {
listen 80;
server_name notes.example.org;
return 301 https://$server_name$request_uri;
}

server {


# SSL configuration

listen 443;
listen [::]:443;
include snippets/ssl-example.org.conf;
include snippets/ssl-params.conf;
server_name notes.example.org;

location / {
auth_basic "Restricted Content";
auth_basic_user_file /path/to/passwd/file;
include uwsgi_params;
uwsgi_pass unix:/path/to/wsgi/socket;
}


}

在 wsgi 下运行的应用程序看到的请求 header ...

Authorization: Basic **************==
Content-Length: 0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Dnt: 1
Host: notes.example.org
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.8
Content-Type:
Accept-Encoding: gzip, deflate, sdch, br

最佳答案

这里您使用的是http基本身份验证选项,在nginx服务器成功反馈后,浏览器会发送用户名:密码的base64。只需使用 python base64 模块即可获取用户名和密码,

>>> from base64 import b64decode
>>> authorization_header = "dXNlcm5hbWU6cGFzc3dvcmQ=" # value from flask request header
>>> details = b64decode("dXNlcm5hbWU6cGFzc3dvcmQ=")
>>> username, password = details.split(':')
>>> username
'username'
>>> password
'password'
>>>

关于nginx - 如何配置 nginx 将用户信息传递到 wsgi/flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41410199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com