简体   繁体   English

Flask session 注销并重定向到登录页面

[英]Flask session log out and redirect to login page

I am using Flask,Python for my web application.我正在为我的 web 应用程序使用 Flask,Python。 The user will login and if the session time is more than 5 minutes then the app should come out and it should land on the login page.用户将登录,如果 session 时间超过 5 分钟,则应用程序应该会出来,它应该登陆登录页面。

I tried some of the methods and I can see the session time out is happening but redirect to login page is not happening.我尝试了一些方法,我可以看到 session 超时正在发生,但重定向到登录页面没有发生。

@app.before_request
def before_request():
    "Session time out method"

    flask.session.permanent = True
    app.permanent_session_lifetime = datetime.timedelta(minutes=2)
    flask.session.modified = True
    flask.g.user = flask_login.current_user
    #return redirect(url_for('login'))

I have used before_request for seesion time out.我已经使用 before_request 来查看超时。 I have referrred this link Flask logout if sessions expires if no activity and redirect for login page but I dont see any changes from what I have tried before and this code.我已经推荐了这个链接Flask 如果会话过期,如果没有活动并重定向登录页面,则注销,但我没有看到我之前尝试过的内容和这段代码有任何变化。 I can see lot of stackoverflow questions over there for this topic and I couldnt find the solution.我可以在那里看到很多关于这个主题的 stackoverflow 问题,但我找不到解决方案。

I have tried this link als Expire session in flask in ajax context我已经尝试过这个链接 als Expire session in flask in ajax context

But I am not sure what should I pass as a session and what qualifier I should return here?但我不确定我应该通过什么作为 session 以及我应该在这里返回什么限定符?

@mod.before_request
def make_session_permanent():
    if session_is_invalid(session):
        return redirect(url_for('logout'))

def session_is_invalid(ses):
    # return your qualifier

if the previous method is correct can some one tell me what is the session and what qualifier I should return here?如果前面的方法是正确的,有人可以告诉我 session 是什么,我应该在这里返回什么限定符?

What I need is after session log out the page should automatically land on the login screen What is happening is session log out is happening but it's not redirecting to login page我需要的是在 session 注销之后页面应该自动登陆登录屏幕发生的事情是 session 正在注销,但它没有重定向到登录页面

could some one help me in this?有人可以帮我吗?

When I read the documents of the Flask-Login package, i saw a few things.当我阅读Flask-Login package 的文档时,我看到了一些东西。 When creating your Flask application, you also need to create a login manager.创建Flask应用程序时,您还需要创建登录管理器。

login_manager = LoginManager()

A login_view variable in LoginManager class caught my attention. LoginManager class 中的login_view变量引起了我的注意。 The details include the following explanation:详细信息包括以下说明:

The name of the view to redirect to when the user needs to log in. (This can be an absolute URL as well, if your authentication machinery is external to your application.)用户需要登录时重定向到的视图名称。(如果您的身份验证机制在您的应用程序外部,这也可以是绝对的 URL。)

Actually, after you create a LoginManager object,实际上,在您创建LoginManager object 之后,

login_manager.login_view = 'your login view'

You should specify your login page.您应该指定您的登录页面。 Finally, Once the actual application object has been created, you can configure it for login with:最后,创建实际应用程序 object 后,您可以将其配置为登录:

login_manager.init_app(app)

After doing these, any unauthorized calls to every method you use @login_required annotation will be sent to the page you have pointed out with login_view .完成这些操作后,对您使用@login_required注释的每个方法的任何unauthorized的调用都将发送到您使用login_view指出的页面。

I also developed a simple application.我还开发了一个简单的应用程序。 You can review this application from here .您可以从此处查看此应用程序。 I tested it and working without any problems.我对其进行了测试并且没有任何问题。

I hope it helps you.我希望它对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM