私はFlaskログインとプリンシパルをIDとロール管理に使用しています。私のニーズは、ドキュメントから直接説明されています。私のコードはここにある:私のログインコードでFlaskのログインとプリンシパル - current_userはログインしていても匿名です
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
# Set the identity user object
identity.user = current_user
# Add the UserNeed to the identity
if hasattr(current_user, 'get_id'):
print 'current_user ' + str(current_user.get_id())
identity.provides.add(UserNeed(current_user.get_id))
# Assuming the User model has a list of roles, update the
# identity with the roles that the user provides
if hasattr(current_user, 'roles'):
if current_user.roles:
for role in current_user.roles:
identity.provides.add(RoleNeed(role.name))
私はこれを行う:予想通りのログインに
identity_changed.send(current_app._get_current_object(),
identity=Identity(user.user_id)
、信号火を。それ以降の各ページの読み込み時には、current_userは匿名であり、ユーザーIDはありませんが、@login_required関数はユーザーがログインしているかのように動作します。Flaskログインはユーザーがログインしていることを認識しますが、何らかの理由でcurrent_userが矛盾します。
どこかの重要な設定点がありませんか?
Flask initで 'static_url_path = '''と何かしていますか?私は同様の問題に遭遇していた参照してください:http://stackoverflow.com/questions/16627384/flask-login-with-static-user-always-yielding-401-unauthorizedそれはセッションとログインのものが大丈夫と見えて困惑していた(ユーザーログインすることができました)しかし、私はいつも匿名のユーザーがセキュリティ保護されたエンドポイントを打つ – craigb