gpt4 book ai didi

python - Pyramid : session 和静态 Assets

转载 作者:太空狗 更新时间:2023-10-29 21:07:32 30 4
gpt4 key购买 nike

让我解释一下问题:

我通过 Pyramid 提供我的静态 Assets :

config.add_static_view(name='static', path='/var/www/static')

而且效果很好。

现在,我有一个在数据库中创建 session 的自定义 session 工厂。它检查浏览器是否提供 session cookie。如果是,它会从数据库中找到一个 session 。如果没有,则在数据库中创建一个新 session ,并向浏览器返回一个 cookie。

到目前为止一切顺利。

现在,在我的 home_view(生成我的主页)中,我不以任何方式访问请求变量:

@view_config(route_name='home', renderer="package:templates/home.mak")
def home_view(request):
return {}

因此,当用户访问主页时,不会在服务器上创建 session 。我认为这是因为 Pyramid lazily 创建 session ——仅当您访问 request.session 时。因此,主页请求的响应 header 不包含 session 的任何 Set-Cookie header 。

现在在我的主页 mako 模板中,我正在为 JavaScript 和 CSS 文件生成静态 URL...

<link rel="stylesheet" href="${request.static_url(...)}"

<script src="${request.static_url(...)}"></script>

现在,由于我正在为来自 Pyramid 的静态 Assets 提供服务,因此对这些 Assets 的所有请求都会通过整个 Pyramid 机器。

因此,当我的浏览器发送获取静态 Assets 的请求时,Pyramid 将如何创建 session 。也就是说,Pyramid 在数据库中创建 session ,并在浏览器发送静态 Assets 请求时发回 session cookie。这是问题 #1。

浏览器以并行的方式发送对静态 Assets 的所有请求。我使用的是最新版本的 Firefox 和 Chrome。由于对实际 HTML 文档的 HTTP 请求没有返回任何 Set-Cookie header ,因此对静态 Assets 的请求没有任何 cookie header 。这意味着 Pyramid 看不到任何请求的 session cookie,它会在数据库中为它为静态 Assets 获取的每个请求创建一个新 session 。

如果我在我的主页上获取 7 个静态 Assets ,则会创建 7 个 session 条目。这是因为所有这些请求都与服务器并行进行并且没有 session cookie,因此 Pyramid 为每个请求创建一个 session 。

如果我故意将 session 作为主页请求的一部分进行访问,则不会出现此问题。它在数据库中创建一个 session 并向浏览器发送一个 cookie,然后浏览器针对它从服务器(并行)请求的每个静态 Assets 发回该 cookie。

@view_config(route_name='home', renderer="package:templates/home.mak")
def home_view(request):
if request.session: pass
return {}

我应该如何防止在静态 Assets 请求上创建 session 。更好的是,我希望 Pyramid 在收到静态 Assets 请求时甚至不接触 session 工厂——这可能吗?

其次,我不明白为什么 Pyramid 在静态请求上创建新 session ?

更新

这是 session 工厂。

def DBSessionFactory(
secret,
cookie_name="sess",
cookie_max_age=None,
cookie_path='/',
cookie_domain=None,
cookie_secure=False,
cookie_httponly=False,
cookie_on_exception=True
):

# this is the collable that will be called on every request
# and will be passed the request
def factory(request):
cookieval = request.cookies.get(cookie_name)
session_id = None
session = None

# try getting a possible session id from the cookie
if cookieval is not None:
try:
session_id = signed_deserialize(cookieval, secret)
except ValueError:
pass

# if we found a session id from the cookie
# we try loading the session
if session_id is not None:
# _load_session will return an object that implements
# the partial dict interface (not complete, just the basics)
session = _load_session(session_id)

# if no session id from cookie or no session found
# for the id in the database, create new
if session_id is None or session is None:
session = _create_session()

def set_cookie(response):
exc = getattr(request, 'exception', None)
if exc is not None and cookie_on_exception == False:
return
cookieval = signed_serialize(session.session_id, secret)
response.set_cookie(
cookie_name,
value=cookieval,
max_age = cookie_max_age,
path = cookie_path,
domain = cookie_domain,
secure = cookie_secure,
httponly = cookie_httponly,
)

def delete_cookie(response):
response.delete_cookie(
cookie_name,
path = cookie_path,
domain = cookie_domain,
)

def callback(request, response):
if session.destroyed:
_purge_session(session)
delete_cookie(response)
return

if session.new:
set_cookie(response)

# this updates any changes to the session
_update_session(session)


# at the end of request
request.add_response_callback(callback)

# return the session from a call to the factory
return session

# return from session factory
return factory

然后,

factory = DBSessionFactory('secret')
config.set_session_factory(factory)

更新

我的自定义身份验证:

class RootFactory:
__acl__ = [
(Allow, Authenticated, 'edit'),

# only allow non authenticated users to login
(Deny, Authenticated, 'login'),
(Allow, Everyone, 'login'),
]

def __init__(self, request):
self.request = request



class SessionAuthenticationPolicy(CallbackAuthenticationPolicy):
def __init__(self, callback=None, debug=False):
self.callback = callback
self.debug = debug

def remember(self, request, principal, **kw):
return []

def forget(self, request):
return []

def unauthenticated_userid(self, request):
if request.session.loggedin:
return request.session.userid
else:
return None

然后,

config.set_root_factory(RootFactory)

config.set_authentication_policy(SessionAuthenticationPolicy())
config.set_authorization_policy(ACLAuthorizationPolicy())

最佳答案

我无法在虚拟项目中重现此行为,这使我相信您的某些配置会影响此处未显示的内容。显然,如果调用任何身份验证,将根据您的身份验证策略创建一个 session 。静态 Assets (默认情况下)使用 NO_PERMISSION_REQUIRED 注册,这意味着它们不会调用 Pyramid 中的任何身份验证 API(我已经验证是这种情况)。

对静态 Assets 的请求确实会调用整个请求管道,这意味着如果您在任何订阅者中有任何代码,或者您的根工厂调用 has_permission 或其他安全 API,或者直接接触 session 本身,那么这将解释您所看到的行为,因为您的 session 与您的身份验证相关联。

关于python - Pyramid : session 和静态 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15643798/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com