你想要的其实很容易实现。在您的verify_password
回拨中,当用户不提供凭证时,您将获得设置为''
的用户名和密码。您仍然可以从该功能返回True
,这将允许匿名用户访问端点。
下面的例子演示了这种方法:
auth = HTTPBasicAuth()
@auth.verify_password
def verify_password(username, password):
if username == '' or password == '':
# anonymous user, we still let them in
g.current_user = None
return True
g.current_user = my_verify_function(username, password)
return g.current_user is not None
@app.route("/clothesInfo")
@auth.login_required
def show_info():
if g.current_user:
# prepare data for authenticated users here
pass
else:
# prepare data for anonymous users here
pass
return jsonify(data)
我不知道,如果你需要传递给你的模板中包含的信息,如果用户身份验证的对象,你可以做到这一点与烧瓶httpauth。我知道你一定可以用[Flask-Login](https://flask-login.readthedocs.io/en/latest/)来做到这一点,请看这里http://stackoverflow.com/questions/18361151/using-flask- login-session-with-jinja2-templates – MrLeeh