2

我正在尝试使用我编写的以下装饰器对 bottle.py 进行 HTTP 基本身份验证:

def check_auth(username, password):
if username == 'admin' and password == 'pass':
    return True
else:
    return False

def authenticate(msg_string = "Authenticate."):
response.content_type = "application/json"
message = {'message': msg_string}
resp = jsonpickle.encode(message)
response.status = "401 - Unauthorized"
response.headers['WWW-Authenticate'] = 'Basic realm="PyBit"'

return resp

def requires_auth(f):
def decorated(*args, **kwargs):
    print request.auth
    auth = request.auth
    if not auth: 
        return authenticate()
    elif not check_auth(auth[0],auth[1]):
        response.status = "401 - Unauthorized"
        return authenticate("HTTP Authentication Failed.")
    else:
        return f(*args, **kwargs)
return decorated

它可以在内置的 wsgiref 服务器中运行,但当我使用 mod_wsgi 在 Apache 下运行我的应用程序时却不行。在这种情况下,“auth”对象始终为“None”。

阿帕奇在捏它吗?

4

1 回答 1

3

没关系。排序它。默认情况下不传递授权标头。我们需要设置 WSGIPassAuthorization 指令来控制当等效的 HTTP 请求标头存在时,是否将 HTTP 授权标头传递给 WSGI 应用程序环境的 HTTP_AUTHORIZATION 变量中的 WSGI 应用程序。

于 2012-11-14T10:44:42.393 回答