简体   繁体   中英

Flask: Login without authentication

I have a Flask application that I need to have a login for but I do not need authentication. The username and password fields need to be sent to an already created function that signs in the user to a netscaler. This function returns a session key. Also if multiple people are accessing the web application, how can I keep each of their sessions separate? I do not want to register any users as that is not needed.

Since you already have your login functionality and simple need to keep the session key around to differentiate between users, use the session proxy:

session['key'] = your_login_function(request.form.get('username'), request.form.get('password'))

To check if the user is logged in, check that key in session :

if 'key' in session:
    # there's a logged-in user

app.before_request is a great place for this as it's called before each request:

@app.before_request
def _check_user_login():
    if not request.path.startswith('/static/') and \
            not request.path in ['/urls', '/that', '/do', '/not', '/require', '/login']:
        if not 'key' in session:
            return redirect(url_for('your_login_page'))

To restrict how long the user can stay logged in under that key, set the session's permanent value to True and configure permanent_session_lifetime to be how long you want the login to be valid

app.config['PERMANENT_SESSION_LIFETIME'] = # ...

session['key'] = # ...
session.permanent = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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