在金字塔中,使用烧杯进行会话,如何才能使某些响应不包含Cookie?
目前,如果我在我的应用程序上卷曲任何网址,我会收到类似的内容:
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Thu, 07 Nov 2013 02:14:45 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 776
Connection: keep-alive
Set-Cookie: beaker.session.id=0a6d945c09884ca29d73bc4ff4d09ff0; expires=Thu, 07-Nov-2013 03:14:45 GMT; httponly; Path=/; secure
我不需要为所有请求设置cookie。例如,我想将其从具有子域“api”的请求中删除。我尝试过改变:
def main(global_config, **settings):
session_factory = session_factory_from_settings(settings)
config = Configurator(settings=settings, root_factory=get_root)
config.set_session_factory(session_factory)
return config.make_wsgi_app()
为:
def main(global_config, **settings):
session_factory = session_factory_from_settings(settings)
config = Configurator(settings=settings, root_factory=get_root)
#config.set_session_factory(session_factory)
return MethodOverride(config, session_factory)
class MethodOverride(object):
def __init__(self, config, session_factory):
import copy
self.config = copy.deepcopy(config)
config.set_session_factory(session_factory)
self.application = config.make_wsgi_app()
def __call__(self, environ, start_response):
if "api" == environ['HTTP_HOST'].split('.')[0]:
self.application = self.config.make_wsgi_app()
我认为这会使会话工厂不会在这些实例中设置,因此没有cookie。我不太了解中间件发生了什么。我也可以找到一种方法使其成为具有“application / json”mimetype的Response对象不包含该cookie。任何帮助将不胜感激。
答案 0 :(得分:1)
您可以通过使用NewResponse
subscriber修改传出响应来实现此目的。
例如:
def new_response_subscriber(event):
request = event.request
response = event.response
if "api" == request.environ['HTTP_HOST'].split('.')[0]:
if 'Set-Cookie' in response.headers:
del response.headers['Set-Cookie']
这是从所有回复中删除所有Cookie的一种方法。另一种方法是创建一个新的会话工厂,检查当前URL是否是API请求,如果是,则根本不创建会话。