python - Flask 中的简单例份验证在 Apache 下不起作用

标签 python apache authentication flask

我正在使用 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.

http://flask.pocoo.org/snippets/8/

关于python - Flask 中的简单例份验证在 Apache 下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561085/

相关文章:

python - 将字典值转换为字典键值对

python - 调用 Request.finish 后在请求上调用 Twisted 错误 : Request. write

php - MP3流/下载网站-Apache服务器内存问题

php - 在php解释器解析打开数据库连接、执行curl请求等代码后,后台实际发生了什么?

PHP 登录不起作用

google-app-engine - 尝试从 Google App Engine Launcher 部署应用程序时出现错误 401

python - pandas DataFrame风格,高亮nan的

python - 在球形三角网格中查找包含点的三角形(Python,球坐标)

java - 如何在 Java 中也读取具有空值的 Excel 单元格......?

authentication - 使用 jQuery AJAX 进行 CAS 身份验证和重定向