我是一個新手 web 開發人員,但經驗豐富的 python 程式員和 Apache dolt。最近,我一直在修補托管一個小型網站,并通過一些托管問題、Flask、html 模板等學習自己的方法。
我已經遵循了幾個 Flask 教程,這些教程是關于控制對帶有@login_required訪問控制端點上的裝飾器的頁面的訪問以及session用于存盤登錄的 kv 對的。當在本地機器上的 Flask 開發服務器上本地運行時,這一切都完美無缺。但是,當我將它推送到我的托管服務上時,我得到了許多訪問控制端點的快取行為,并且在注銷后我能夠看到它們(并檢查會話資料以確保密鑰已移除)。
一些具體...
使用
flaskwithsession登錄資訊,而不是flask-login。在托管 VPS 上托管,該 VPS 使用 Phusion 乘客作為 Apache 的 WSGI 介面
我沒有用于 Apache 的組態檔......現在只是默認值。
網站流量非常低......現在只有我和機器人。:)
我的passenger_wsgi檔案:
import sys, os
from datetime import timedelta
INTERP = "/home/<website>/venv1/bin/python3"
#INTERP is present twice so that the new Python interpreter knows the actual executable path
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
# def application(environ, start_response):
# start_response('200 OK', [('Content-type', 'text/plain')])
# return ["Hello, world!"]
sys.path.append(os.getcwd())
from app import app as application
登錄后,事情按預期進行。注銷后,我仍然可以訪問應該受到訪問控制的端點,并且當我在瀏覽器中檢查網路流量時,我反復看到這個“證據”:
Summary
URL: https://<website>/<endpoint> <---- an endpoint covered by @login_required
Status: 200
Source: Memory Cache
Request
No request, served from the memory cache.
Response
Content-Type: text/html; charset=utf-8
Expires: Wed, 22 Dec 2021 17:14:00 GMT
Date: Wed, 22 Dec 2021 17:04:00 GMT
Content-Length: 23
Cache-Control: max-age=600
Vary: User-Agent
Status: 200 OK
x-powered-by: Phusion Passenger 5.0.30
Server: Apache
所以我的問題是這些......
- 我的診斷是否正確,這是 Apache 快取在作業?(證據看起來很有說服力... :) )
- 我寧愿不(此時)投入精力轉向,
flask-login除非這是治愈性的。 - 有沒有一種簡單的方法來控制這種行為,而無需成為 apache 或Passenger 組態檔等方面的專家?如果這對于這個低流量站點可行,我不介意關閉快取,但我會對一個好的解決方案感興趣,以便教育自己控制快取或以某種方式告訴 apache(? ) 哪些端點是訪問控制的,等等。
我謙虛地將我的問題提交給處理這些堆疊的人!
uj5u.com熱心網友回復:
從 5.0 開始,乘客將“有幫助地”將快取控制標頭添加到它認為“可快取”的回應中。
為了阻止這種情況,您的應用程式應該添加 headerCache-Control: no-store。
要在 Flask 中全域執行此操作,如下所述:
@app.after_request
def add_header(response):
# response.cache_control.no_store = True
if 'Cache-Control' not in response.headers:
response.headers['Cache-Control'] = 'no-store'
return response
如果您想更具辨別力并且只想對需要登錄的路由執行此操作,則可以對login_required裝飾器進行自己的擴展,以便為需要登錄的路由(或完全單獨的裝飾器)執行此操作
from flask import make_response
from flask_login import login_required as _login_required
def login_required(f):
# apply the usual login_required decorator
decorated = _login_required(f)
def cache_no_store_login_required_view(*args, **kwargs):
resp = make_response(decorated(*args, **kwargs))
# add the cache-control header to the response
resp.headers['Cache-Control'] = 'no-store'
return resp
return cache_no_store_login_required_view
或者作為一個單獨的裝飾器來設定 no-store ...
from functools import wraps
def cache_no_store(f):
@wraps(f)
def decorated_view(*args, **kwargs):
resp = make_response(f(*args, **kwargs))
resp.headers['Cache-Control'] = 'no-store'
@app.route('/protected')
@cache_no_store
@login_required
def my_route():
# ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/399692.html
上一篇:Jest的“constmock=jest.fn();”是什么意思?真的嗎?
下一篇:Haskell中負數的模式匹配
