Używam login i dyrektor Flask do zarządzania tożsamością i rolą. Moje potrzeby są opisane wprost z dokumentów. Mój kod jest tutaj:Logowanie i główne logowanie w kolbie - użytkownik current_user jest anonimowy, mimo że jestem zalogowany
@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))
W moim kodu logowania to zrobić:
identity_changed.send(current_app._get_current_object(),
identity=Identity(user.user_id)
Po zalogowaniu ognie sygnałowe, jak oczekiwano. Przy każdym kolejnym ładowaniu strony użytkownik current_user jest anonimowy i nie ma identyfikatora użytkownika, ale wszystkie funkcje @login_required zachowują się tak, jakby użytkownik był zalogowany. Logowanie przy użyciu Flask wie, że użytkownik jest zalogowany, ale z jakiegoś powodu użytkownik current_user jest niespójny.
Czy brakuje mi gdzieś istotnego punktu konfiguracji?
Czy robisz cokolwiek z 'static_url_path = ''' w twoim instrumencie Flask? Wystąpił podobny problem, patrz: http://stackoverflow.com/questions/16627384/flask-login-with-static-user-always-riielding-401-nauthorized Było kłopotliwe, ponieważ sesja i logowanie wyglądały dobrze (użytkownik mógł się zalogować), ale zawsze miałem anonimowego użytkownika podczas uderzania w zabezpieczony punkt końcowy – craigb