Python Flask中的简单身份验证在Apache下不起作用
我正在用Flask构建一个网站,我现在想用一个非常简单的身份验证机制来保护管理员视图。为此,我编写了以下包装器代码:Python Flask中的简单身份验证在Apache下不起作用,python,apache,authentication,flask,Python,Apache,Authentication,Flask,我正在用Flask构建一个网站,我现在想用一个非常简单的身份验证机制来保护管理员视图。为此,我编写了以下包装器代码: def check_auth(username, password): current_app.logger.error('Log from check_auth') return username == 'myusername' and password == 'mypassword' def authenticate(): current_app.l
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'
如您所见,auth
(应包含填写的用户名和密码)仍然为空。奇怪的是,登录屏幕一显示,这三个日志就已经显示出来了。这意味着函数将继续执行,而不是等待用户填写用户名和密码
有人知道我做错了什么吗?为什么它可以与Flask开发服务器一起工作,但不能与Apache/mod_wsgi一起工作?欢迎所有提示 我认为这会有帮助: 如果您在mod_wsgi中使用basic auth,则必须启用auth转发,否则apache将使用所需的头并不会将其发送到您的应用程序:WSGIPassAuthorization
很好。我是Apache服务器管理新手,请耐心听我说。我在文件
/etc/apache2/apache2.conf
中搜索了WSGIPassAuthorization
,但它当前不在文件中。我只需要将它添加到底部的文件中并重新启动Apache,还是需要在其他地方设置它?这取决于您的配置,但我认为是的,您可以在底部添加此指令:最后,我将它放在我网站的Apache配置文件中,该文件位于/etc/apache2/sites available/mysite.com.conf
中。它现在运行得很好。万分感谢!
Log from requires_auth function
None
Log from authenticate function