【发布时间】:2015-10-12 04:59:01
【问题描述】:
我正在用 Flask 构建一个网站,现在我想用一个非常简单的身份验证机制来保护一个管理员视图。为此,我编写了以下包装代码:
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 一起使用?欢迎所有提示!
【问题讨论】:
标签: python apache authentication flask