我正在運行Flask網絡應用程序並使用Apache基本身份驗證(使用.htaccess和.htpasswd文件)來密碼保護它。我想密碼只保護應用程序中的一個網頁。當我用密碼保護網頁的html文件時,沒有任何效果,網頁仍然沒有密碼保護。難道這是因爲它是我的python文件使用render_template調用html文件?我不知道如何解決這個問題。密碼保護Flask應用程序中的一個網頁
回答
您需要限制對端點的訪問。 This snippet應該讓你開始正確的道路。
from functools import wraps
from flask import request, Response
def check_auth(username, password):
"""This function is called to check if a username/
password combination is valid.
"""
return username == 'admin' and password == 'secret'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
有了這個,你可以裝飾你想要@requires_auth
限制任何端點。
@app.route('/secret-page')
@requires_auth
def secret_page():
return render_template('secret_page.html')
我實際上使用的是類似於以前的方法。我用這種方法的問題是,當我輸入用戶名和密碼時,它只是再次提示他們,它永遠不會驗證。 – accelke
從片段:如果您使用mod_wsgi進行基本身份驗證,則必須啓用身份驗證轉發,否則apache將使用所需的頭文件並且不會將其發送到您的應用程序:[WSGIPassAuthorization](https://code.google.com/ p/modwsgi /維基/ ConfigurationDirectives#WSGIPassAuthorization)。 – dirn
是的,我確實在我的/etc/apache2/sites-available/app.com.conf文件中添加了「WSGIPassAuthorization On」,但它沒有任何區別。是否有其他文件需要放入? – accelke
- 1. 密碼保護一個網頁
- 2. 受密碼保護的應用程序
- 3. 密碼保護網頁
- 4. 使用密碼保護應用程序
- 5. 密碼保護應用程序開始
- 6. 密碼保護iPhone應用程序
- 7. 卸載應用程序密碼保護
- 8. 如何密碼保護應用程序
- 9. 密碼保護應用程序按鈕
- 10. 密碼保護iPhone應用程序
- 11. 密碼保護Winforms應用程序
- 12. 密碼保護asp.net應用程序?
- 13. 使用php的密碼保護網頁
- 14. 密碼保護的Android應用程序。重置密碼功能?
- 15. 密碼保護小程序
- 16. 基本的PHP密碼保護網頁
- 17. 網站上的密碼保護頁面
- 18. ASP.NET網頁的密碼保護?
- 19. 加載密碼保護的網頁
- 20. 密碼保護一個asp.net網站使用登錄頁
- 21. Android應用程序中的保護用戶名和密碼
- 22. 保護桌面應用程序中的用戶密碼
- 23. PHP密碼保護網頁 - 重定向
- 24. 密碼保護我的iPad應用程序中的按鈕
- 25. 使用唯一密碼在Laravel中密碼保護頁面
- 26. 密碼保護IIS 7.5中的ASP.NET Web應用程序
- 27. 在tomcat中受密碼保護的應用程序
- 28. 如何在javascript中創建密碼保護的應用程序?
- 29. 密碼保護整個.net mvc應用程序?
- 30. 密碼保護整個Django應用程序
你是什麼意思「密碼保護html文件」? – dirn
我的意思是我添加了.htaccess和.htpasswd文件,並在.htaccess文件中指定了html文件。訪問文件時,這些文件應詢問用戶名和密碼。 – accelke
這隻有在您通過Apache直接提供HTML文件時纔有效。您需要限制對Flask中端點的訪問。 – dirn