我正在使用 Flask 构建一个网站,我现在想在其中使用非常简单的身份验证机制来保护管理员 View 。为此,我编写了以下包装器代码:
def check_auth(username, password):
current_app.logger.error('Log from check_auth')
return username == 'myusername' and password == 'mypassword'
def authenticate():
current_app.logger.error('Log from authenticate function')
return Response('Bad luck my friend.', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
current_app.logger.error('Log from requires_auth function')
auth = request.authorization
current_app.logger.error(auth) # <= HERE I LOG auth
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@requires_auth
def some_view():
return 'some stuff'
这在使用 Flask 开发服务器时工作正常。我刚刚在 Apache/mod_wsgi 上部署了它,但不幸的是现在它不起作用;填写我的登录详细信息后,它只是重新加载登录屏幕(提示密码错误)。
我在那里放了一些日志,现在它记录了以下内容:
Log from requires_auth function
None
Log from authenticate function
如您所见,auth
(应包含填写的用户名和密码)保持为 None。奇怪的是,这三个日志在登录界面一出现就显示出来了。这意味着该函数不会等待用户填写他的用户名和密码,而是继续执行。
有人知道我在这里做错了什么吗?为什么它可以与 Flask 开发服务器一起使用,但不能与 Apache/mod_wsgi 一起使用?欢迎所有提示!
最佳答案
我认为这会有所帮助:
If you are using basic auth with mod_wsgi you will have to enable auth forwarding, otherwise apache consumes the required headers and does not send it to your application: WSGIPassAuthorization.
关于python - Flask 中的简单例份验证在 Apache 下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561085/