0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

flaskで運用してるサービスににbasic認証をつけた日

Last updated at Posted at 2019-12-16

これを参考にした。

| #必要なライブラリのインポート |
|:--|
| from flask import Flask |
| from flask_httpauth import HTTPBasicAuth #HTTP"Basic"Auth |
|  |
| #Flask、HTTPBasicAuthクラスのインスタンスを作成 |
| app = Flask(__name__) |
| auth = HTTPBasicAuth() |
|  |
| #"id":"パスワード" |
| id_list = { |
|     "Tanaka": "1111", |
|     "Suzuki": "1234" |
| } |
|  |
| #入力されたidに該当するパスワードを |
| #比較のために取得する |
| @auth.get_password |
| def get_pw(id): |
|     if id in id_list: |
|         return id_list.get(id) |
|     return None |
|  |
| #実際の処理部分 |
| @app.route('/') |
| @auth.login_required #ここで認証が行われる |
| #認証に成功したら以下の処理を実行する |
| def index():  |
|     return "Hello, %s!" % auth.username() |
|  |
| if __name__ == '__main__': |
|     app.run() |コード

やったこと

もう動いているものがあったので、
①必要なライブラリをimportしたら、

@app.route('/') 

↑の処理部分直前に

@auth.login_required

を挟む。

②ID/PASSを任意のものに修正。

参考

Raykeymas/flask-httpauth-basic.py

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?