我无法使用Flask,Apache和mod_wsgi获得Windows机器的用户名

时间:2019-02-06 22:06:09

标签: windows authentication flask mod-wsgi wsgi

我在装有Apache24和Python3.6的服务器中有一个包含Flask的站点。我需要获取进入该站点的Windows计算机的用户名。

我已经阅读了多个站点和答案,但是它对我不起作用。我正在使用mod_wsgi并启用了“ WSGIPassAuthorization On”,在此之后,我尝试获取REMOTE_USER变量,但是它不起作用。另外,我在web.wsgi中添加了一些代码,以查看Apache发送给我的变量,在那里我也看不到用户名。

#Apache httpd.conf configuration for the site
LoadFile "d:/Anaconda3/python36.dll"
LoadModule wsgi_module "d:/anaconda3/lib/site-packages/mod_wsgi-4.6.4-py3.6-win-amd64.egg/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "d:/anaconda3"
WSGIPassAuthorization On
WSGIScriptAlias / "D:/wwwApache/visor/web.wsgi"

#WSGIRestrictStdout Off
#WSGIScriptReloading On
#WSGIApplicationGroup %{GLOBAL}
<Directory "D:/wwwApache/visor">
    Require all granted
</Directory>



#app.py file piece of code.
from flask import request

@app.route('/')
def index():
    #Create file that it's going to contain the environ variables
    f= open("variables.txt","w+")
    for header in request.headers.items():
    f.write(str(header)+"\n")
    #Check if REMOTE_USER exists.
    f.write(str(request.environ.get('REMOTE_USER')))
    f.close()
    return render_template('index.html')



#web.wsgi File
import sys
import pprint
sys.path.insert(0, 'D:/site/')
from app import app as application


#This is to get the environment variables, also REMOTE_USERs.
#When it get the variables, it will send the result to error logs of Apache.
class RemoteUserMiddleware(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        user = environ.pop('HTTP_X_PROXY_REMOTE_USER', None)
        environ['REMOTE_USER'] = user
        return self.app(environ, start_response)


class LoggingMiddleware:

    def __init__(self, application):
        self.__application = application

    def __call__(self, environ, start_response):
        errors = environ['wsgi.errors']
        pprint.pprint(('REQUEST', environ), stream=errors)

        def _start_response(status, headers, *args):
            pprint.pprint(('RESPONSE', status, headers), stream=errors)
            return start_response(status, headers, *args)

        return self.__application(environ, _start_response)

application = LoggingMiddleware(application)

直到现在我都无法获得用户,我已经读过一个选项是安装IIS,但是我还不想这样做。谢谢!

0 个答案:

没有答案