簡體   English   中英

Flask基本身份驗證不接受用戶名和密碼

[英]Flask basic authentication not accepting username and password

我在服務器上的flask應用程序中使用BasicAuth。 每當我嘗試輸入用戶名和密碼時,應用程序只會再次提示我輸入信息,但它不會對我進行身份驗證。 我不知道如何解決這個問題。 我的應用程序中的代碼是:

app.config['BASIC_AUTH_USERNAME'] = 'user'
app.config['BASIC_AUTH_PASSWORD'] = 'password'

basic_auth = BasicAuth(app)
@app.route("/secret")
@basic_auth.required
def secret_view():
    return submit.html

我在這個 AWS論壇上找到了答案。 問題在於Elastic Beanstalk配置:您必須配置Apache以將身份驗證標頭(即用戶的明文用戶名和密碼)傳遞給您的python應用程序(因此,您使用任何解析身份驗證憑據的解決方案) ,在我的情況下flask_httpauth )通過mod_wsgi

為此,請在.ebextensions文件中保存的.config文件中設置容器命令。 我使用下面的那個。

.ebextensions / auth.config

container_commands:
  01_wsgi_pass_headers:
    command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'

當然,不言而喻,由於這些憑據將以明文形式傳遞,因此您必須將應用程序設置為僅通過HTTPS連接進行訪問!

這個簡單的例子對我有用:

app.py

from flask import Flask, render_template
from flask.ext.basicauth import BasicAuth

app = Flask(__name__)

app.config['BASIC_AUTH_USERNAME'] = 'user'
app.config['BASIC_AUTH_PASSWORD'] = 'password'

basic_auth = BasicAuth(app)
@app.route("/secret")
@basic_auth.required
def secret_view():
    return render_template('secret_page.html')

if __name__ == '__main__':
    app.run(debug=True)

模板/ secret_page.html

Secret page!

樣品要求:

curl -I http://localhost:5000/secret

HTTP/1.0 401 UNAUTHORIZED
WWW-Authenticate: Basic realm=""
Content-Type: text/html; charset=utf-8
Content-Length: 0
Server: Werkzeug/0.10.4 Python/2.7.8
Date: Wed, 15 Apr 2015 04:42:42 GMT

curl -I -u user:password http://localhost:5000/secret
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 12
Server: Werkzeug/0.10.4 Python/2.7.8
Date: Wed, 15 Apr 2015 04:42:54 GMT

curl -u user:password http://localhost:5000/secret
Secret page!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM