在 flask 中,我使用以下 snippet启用 HTTP 身份验证:
def authenticate():
return Response('<Why access is denied string goes here...>', 401, {'WWW-Authenticate':'Basic realm="Login Required"'})
现在,根据我过去使用 Flask 的经验,如果有人的凭据不正确,我想让他们知道我可以打电话:
abort(401)
这为您提供了基本的 apache 401 响应。有谁知道我如何使用上面的代码片段来实现它?
谢谢
最佳答案
在 Flask 中自定义错误响应非常简单。创建一个函数,其唯一参数是 HTTP 错误状态码,使其返回一个 flask.Response 实例,并用 @app.errorhandler 装饰它.
@app.errorhandler(401)
def custom_401(error):
return Response('<Why access is denied string goes here...>', 401, {'WWW-Authenticate':'Basic realm="Login Required"'})
然后您可以使用 abort(401)
来满足您的需求。
关于python - 在 flask 中使用 HTTP 身份验证时的标准 401 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7877230/