繁体   English   中英

python/wsgi中GET请求授权header

[英]Authorization header of GET request in python/wsgi

我正在 Python 中创建 POST/GET API 3. 我正在运行连接到 WSGI 脚本的 Apache2。 我成功地检索了非常简单的 GET 请求。 到目前为止我的代码:

def application(environ, start_response):   
    status = '200 OK'
    output = b'Hello'
    
    print(environ)
    # print(environ['HTTP_AUTHORIZATION'])

    response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

我使用reqbin测试发送 GET 请求到我的服务器。 当您在不记名令牌字段中输入令牌时,它会自动添加到标头中。 我用我有承载令牌的服务器测试了这个并且验证成功完成,所以我知道 reqbin 实际上正在发送令牌。

但是,我好像无法访问我服务器上的授权header。 显然,它应该在以 HTTP_ 为前缀的环境 object 内。 但是打印environ['HTTP_AUTHORIZATION']产生 KeyError。 然后我尝试打印完整的环境 object 并从 apache 日志中检索它:

{
    'mod_wsgi.listener_port': '443',
    'CONTEXT_DOCUMENT_ROOT': '/var/www/gosharing',
    'SERVER_SOFTWARE': 'Apache/2.4.41 (Ubuntu)',
    'SCRIPT_NAME': '',
    'mod_wsgi.enable_sendfile': '0',
    'mod_wsgi.handler_script': '',
    'SERVER_SIGNATURE': '<address>Apache/2.4.41 (Ubuntu) Server at domain.ext Port 443</address>\\n',
    'REQUEST_METHOD': 'GET',
    'PATH_INFO': '/',
    'SERVER_PROTOCOL': 'HTTP/1.1',
    'QUERY_STRING': '',
    'wsgi.errors': <mod_wsgi.Log object at 0x7f0b517c0c10>,
    'HTTP_X_REAL_IP': '2a02:a44a:ea1e:1:9053:2c7a:daaa:16',
    'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36',
    'SERVER_NAME': 'domain.ext',
    'REMOTE_ADDR': '206.189.205.251',
    'mod_wsgi.queue_start': '1644325870796726',
    'mod_wsgi.request_handler': 'wsgi-script',
    'apache.version': (2, 4, 41),
    'mod_wsgi.version': (4, 6, 8),
    'wsgi.url_scheme': 'https',
    'PATH_TRANSLATED': '/var/www/gosharing/gosharing.wsgi/',
    'SERVER_PORT': '443',
    'mod_wsgi.total_requests': 0L,
    'wsgi.multiprocess': False,
    'SERVER_ADDR': '185.45.113.35',
    'DOCUMENT_ROOT': '/var/www/gosharing',
    'mod_wsgi.process_group': 'gosharing',
    'mod_wsgi.thread_requests': 0L,
    'mod_wsgi.daemon_connects': '1',
    'mod_wsgi.request_id': 'sn1scyGWCVM',
    'SCRIPT_FILENAME': '/var/www/gosharing/gosharing.wsgi',
    'SERVER_ADMIN': 'webmaster@localhost',
    'mod_wsgi.ignore_activity': '0',
    'wsgi.input': <mod_wsgi.Input object at 0x7f0b48f01030>,
    'HTTP_HOST': 'domain.ext',
    'CONTEXT_PREFIX': '',
    'wsgi.multithread': True,
    'mod_wsgi.callable_object': 'application',
    'mod_wsgi.daemon_restarts': '0',
    'REQUEST_URI': '/',
    'HTTP_ACCEPT': '*/*',
    'mod_wsgi.path_info': '/',
    'wsgi.file_wrapper': <type 'mod_wsgi.FileWrapper'>,
    'wsgi.version': (1, 0),
    'GATEWAY_INTERFACE': 'CGI/1.1',
    'wsgi.run_once': False,
    'mod_wsgi.script_name': '',
    'REMOTE_PORT': '39762',
    'mod_wsgi.listener_host': '',
    'REQUEST_SCHEME': 'https',
    'SSL_TLS_SNI': 'domain.ext',
    'wsgi.input_terminated': True,
    'mod_wsgi.script_start': '1644325870815229',
    'mod_wsgi.application_group': '',
    'mod_wsgi.script_reloading': '1',
    'mod_wsgi.thread_id': 1,
    'mod_wsgi.request_start': '1644325870796210',
    'HTTP_ACCEPT_ENCODING': 'deflate, gzip',
    'mod_wsgi.daemon_start': '1644325870800682'
}

事实上,我可以在 reqbin 上添加任何 header,并且能够在我的 apache 日志中看到它,除了授权 header。也许它在一个更受保护的地方? 请帮帮我。

我想到了。 在您的000-default-le-ssl.conf000-default.conf文件中(取决于您是否使用安全连接),您应该通过在VirtualHost标记内写入WSGIPassAuthorization On来手动打开授权传递:

<VirtualHost *:443> # or port 80 if you are using an insecure connection
    # [...]

    WSGIPassAuthorization On

    # [...]

</VirtualHost>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM