拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Flask登录与Apache快取

Flask登录与Apache快取

白鹭 - 2022-01-23 2004 0 0

我是一个新手 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

所以我的问题是这些......

  1. 我的诊断是否正确,这是 Apache 快取在作业?(证据看起来很有说服力... :) )
  2. 我宁愿不(此时)投入精力转向,flask-login除非这是治愈性的。
  3. 有没有一种简单的方法来控制这种行为,而无需成为 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():
    # ...
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *