2017-01-16 27 views
0

我使用燒瓶登錄https://flask-login.readthedocs.io/en/latest進行會話管理。用戶首先登錄(login.html)應用程序並轉到home.html。但是,在用戶通過身份驗證並點擊不同的鏈接之後,它將踢出會話並返回到登錄頁面。這是非常隨機發生的,我不確定哪裏出了問題?它使用的是Apache。這似乎與localhost沒關係,但在Apache中有這個問題。是否有特定的apache配置需要注意?flask-login在認證後隨機無效會話

請告知如何解決此問題?非常感謝!!

class User(UserMixin): 
     pass 

    @login_manager.user_loader 
    def load_user(user_id): 
     print "load_user...." + user_id 
     user = User() 
     user.id = user_id 
     return user 

    @app.route("/login", methods=['POST']) 
    def login(): 
     #login procedure 
     curr_user = User() 
     curr_user.id = LOGIN_USERNAME 
     login_user(curr_user) 
     return redirect(url_for('home')) 

    @app.route("/") 
    @app.route("/home") 
    @login_required 
    def home(): 
     return render_template('home.html') 

回答

0

路由/的功能不會驗證用戶的會話,只是返回登錄模板。

要麼修改loading()功能

from flask import url_for 

@app.route("/") 
def loading(): 
    return redirect(url_for('.home')) 

或用戶是否在

from flask_login import current_user 

@app.route("/") 
def loading(): 
    if current_user.is_authenticated: 
     return render_template('home.html') 
    else: 
     return render_template('login.html') 

或者

記錄做驗證取下loading() funtion和/路由添加到home()

@app.route("/") 
@app.route("/home") 
@login_required 
def home(): 
    return render_template('home.html') 

當過的路線//home被請求時,燒瓶驗證登錄(如@login_required裝飾存在)這樣。如果未登錄,將爲LoginManager定義login_view

lm = LoginManager() 
lm.init_app(flask_app) 
lm.login_view = '/login' 
+0

謝謝@frankinsijo。燒瓶登錄多線程?我遇到麻煩,它在登錄後不時得到踢出,不管單個用戶還是多個用戶。任何想法爲什麼?我發佈了http://stackoverflow.com/questions/41731938/does-flask-login-support-multiple-users-and-sessions-concurrency這裏已經有 –

+0

是的,它支持多個會話。通過不時啓用您的意思是用戶自動註銷?如果是這樣,我相信你的應用程序中的一段代碼會使會話無效,除非你修改了'permanent_session_lifetime'值。 – franklinsijo

+0

謝謝franklinsijo。我沒有使用permanent_session_lifetime,所以默認情況下它不應該有任何會話超時?我在這篇文章中更新了代碼,它會隨機運行'@ login_manager.unauthorized_handler'將會話踢回登錄頁面,請幫助看看您的想法。我是否也需要實現'@ login_manager.request_loader'?另外'{{curr_user.id}}'打印無,不是登錄用戶名 –