我正在尝试使用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我的受保护视图:
@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>当我省略了请求中的auth部分时,它会返回我的受保护视图。为什么flask不接受基本的auth头?
发布于 2015-10-18 05:23:00
我现在正在使用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错误。修复了线路,它起作用了。
https://stackoverflow.com/questions/22621810
复制相似问题