1. 程式人生 > >Python程式設計:Flask-BasicAuth實現Authentication登入認證

Python程式設計:Flask-BasicAuth實現Authentication登入認證

通過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(
)

訪問網站的時候,會彈框要求登入驗證

在這裡插入圖片描述