flask-login的简单实用
# encoding: utf-8 from flask import Flask, Blueprint from flask_login import (LoginManager, login_required, login_user,logout_user, UserMixin) app = Flask(__name__) class User(UserMixin): pass app.secret_key = 's3cr3t' login_manager = LoginManager() login_manager.session_protection = 'strong'#会话保护 login_manager.login_view = 'auth.login' #如果未登录的用户尝试访问一个login_required装饰的视图函数, # Flask-Login会闪现一条消息并且重定向到登录视图提醒你登陆。 # (如果未设置登录视图,它将会以401 错误退出) #默认闪现的消息是Please log in to access this page.。 # 要自定义该消息,可以设置LoginManager.login_message。例如:login_manager.login_message = '请登录后访问' #要自定义消息分类的话,可以设置LoginManager.login_message_category。 # 如 login_manager.login_message_category = 'info' login_manager.init_app(app) @login_manager.user_loader def load_user(user_id): return None # url redirect auth = Blueprint('auth', __name__) @auth.route('/login', methods=['GET', 'POST']) def login(): user = User() login_user(user) return "login page" @auth.route('/logout', methods=['GET', 'POST']) @login_required def logout(): logout_user() return "logout page" # test method @app.route('/test') @login_required def test(): return "yes , you are allowed" app.register_blueprint(auth, url_prefix='/auth') if __name__ == '__main__': app.run(debug=True)
官网:http://www.pythondoc.com/flask-login/
http://liuliqiang.info/flask_login_usage_and_more_tech/
https://amberno1111.github.io/2016/03/22/Flask/Flask-Login%E7%9A%84%E4%BD%BF%E7%94%A8/