https://flask-basicauth.readthedocs.io/en/latest/
通过Flask-BasicAuth,提供用户名密码即可实现Authentication(认证)机制
安装
pip install Flask-BasicAuth
代码示例
# -*- coding: utf-8 -*- # @Date : 2018-10-18 # @Author : Peng Shiyu from flask import Flask from flask_basicauth import BasicAuth app = Flask(__name__) app.config['BASIC_AUTH_USERNAME'] = 'admin' app.config['BASIC_AUTH_PASSWORD'] = '123456' # app.config['BASIC_AUTH_FORCE'] = True # 整个站点都验证 basic_auth = BasicAuth(app) @app.route('/') def index(): return "hello world" @app.route('/secret') @basic_auth.required def secret(): return "hello world secret" if __name__ == '__main__': app.run()
访问网站的时候,会弹框要求登录验证