你想要的其实很容易实现。在您的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)