gpt4 book ai didi

python - Flask 登录 request_loader 不工作?

转载 作者:太空狗 更新时间:2023-10-30 00:17:28 25 4
gpt4 key购买 nike

我正在尝试使用 Flask-Login 扩展在我的 Flask 应用程序上实现基本身份验证。按照文档我尝试了一个示例请求加载器,但它给了我一个 401 未经授权的错误,而即将贬值的 header_loader 工作得很好,所以我做错了什么或者 request_loader 中有错误。

我使用以下 request_loader:

login_manager = LoginManager(app)

@login_manager.request_loader
def load_user_from_request(req):
print req.headers # just to see what happens
user = models.Werknemer.query.first() #this user exists, so it always returns a user
return user

我的保护 View :

@app.route("/test")
@login_required
def index():
return 'OK'

还有我的请求码:

import requests
from requests.auth import HTTPBasicAuth

test_req = requests.get('http://localhost:5000/test', auth=HTTPBasicAuth('test', 'test'))
print test_req.content

当我运行我的请求代码时,它返回:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>401 Unauthorized</title>
<h1>Unauthorized</h1>
<p>The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.</p>

当我省略请求的授权部分时,它会返回我的 protected View 。为什么 flask 不接受 Basic auth header ?

最佳答案

我正在服用 Pluralsight's course on this现在我得到了同样的错误。在我更正身份验证设置中的语法后,它起作用了。

# Configure authentication
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'
login_manager.init_app(app)

我输入了最后一行错误并收到 404 错误。修复了线路,它成功了。

关于python - Flask 登录 request_loader 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com