flask-login验证后随机使会话无效 [英] flask-login invalidates session randomly after authentication

查看:50
本文介绍了flask-login验证后随机使会话无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask-login https://flask-login.readthedocs.io/zh/最新用于会话管理.用户首先登录(login.html)应用程序,然后转到home.html.但是,在用户通过身份验证并单击其他链接之后,它将踢出该会话并返回登录页面.这种情况非常随机发生,我不确定出了什么问题?它正在使用apache.本地主机似乎还可以,但是在Apache中有这个问题.是否需要注意特定的Apache配置?

I'm using flask-login https://flask-login.readthedocs.io/en/latest for session management. The user first login in (login.html) the application and go to home.html. However, after the user has authenticated and click different links, it will kick out the session and go back to login page. This happens very randomly and I'm not sure what went wrong? It is using apache. It seems okay with localhost but having this issue in apache. Are there specific apache configurations need to pay attention?

请告知如何解决此问题?非常感谢!

Please advise how to fix this issue? Thanks so much!!

    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')

推荐答案

我知道这是一篇旧文章,但是我发现了这个答案,似乎为我解决了这个问题: https://stackoverflow.com/a/57162593/5424359 基本上,您需要确保将app.secret_key设置为某个秘密密钥.获取密钥的简单方法如下.

I know this is an old post, but I found this answer which seems to have solved it for me: https://stackoverflow.com/a/57162593/5424359 Basically, you need to make sure you set app.secret_key to some secret key. A simple way to get a key is as follows.

>>>import os
>>>os.urandom(24)

获取该数字并将其设置为app.secret_key.确保您没有让应用每次都生成密钥.

Take that number and set app.secret_key to it. Make sure you don't have the app generating a key every time.

这篇关于flask-login验证后随机使会话无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆