- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
让我解释一下问题:
我通过 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/
是否为每个 Shiny session 分配了 session ID/ session key (如果部署在 Shiny 服务器上)?如果是,我如何访问该信息?我已阅读文档here然而上网查了一下,并
我正在使用 this koajs session 模块。 我检查了源代码,但我真的无法理解。 我想知道它保存 session 数据的位置,因为我没有看到创建的文件,并且当服务器重新启动时, sessi
实现高可扩展性的一种方法是使用网络负载平衡在多个服务器之间分配处理负载。 这种方法提出的一个挑战是服务器是否具有状态意识 - 将用户状态存储在“ session ”中。 此问题的一个解决方案是“粘性
在负载平衡服务器的上下文中, session 亲和性和粘性 session 之间有什么区别? 最佳答案 我见过这些术语可以互换使用,但有不同的实现方式: 在第一个响应中发送 cookie,然后在后续响
我希望其他人向我解释哪种方法更好:使用 session 或设计无 session 。我们正在开始开发一个新的 Web 应用程序,但尚未决定要遵循什么路径。 无 session 设计在我看来更可取: 优
现在用户在他的权限中有很多角色,我将允许他点击 href 并在新窗口中扮演另一个角色。每个角色都有自己的 session 。 既然浏览器打开窗口不能用新 session 打开,我必须在服务器端想办法。
我正在尝试为express.js Node 应用程序实现 session 存储我的问题是: 如何删除具有浏览器 session 生命周期的 cookie(根据连接文档标记有 expires = fal
在开始在 golang 中使用 session 之前,我需要回答一些问题 session 示例 import "github.com/gorilla/sessions" var store = ses
我读过 Namespaced Attributes . 我尝试使用此功能: #src/Controller/CartController.php public function addProduct(
我正在努力完成以下工作: 根据用户的类型更改用户的 session cookie 到期日期。 我有一个 CakePHP Web 应用程序,其中我使用 CakePHP session 创建了我的身份验证
这是我在这里的第一个问题,我希望我做对了。 我需要处理一个 Java EE 项目,所以在开始之前,我会尝试做一些简单的事情,看看我是否能做到。 我坚持使用有状态 session Bean。 这是问题:
ColdFusion session 与 J2EE session 相比有什么优势吗? ColdFusion session documentation提到了 J2EE session 的优点,但没有
在执行任何任务之前,我需要准确地在创建 session 时创建一个 session 范围变量(因为我的所有任务都需要一个初始 session 范围变量才能运行)。因为,创建 session 时,gra
我们当前的应用使用 HTTP session ,我们希望将其替换为 JWT。 该设置仅允许每个用户进行一次 session 。这意味着: 用户在设备 1 上登录 用户已在设备 1 上登录(已创建新 s
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
假设我在两个或更多设备上打开了两个或更多用户 session (同一用户没有管理员权限)。 在当前 session 中,如果我注销,是否意味着所有其他 session 也会关闭?如果没有,有没有办法通
我正在评估在 tomcat 中使用带有 session 复制的粘性 session 的情况。根据我的初步评估,我认为如果我们启用 session 复制,那么在一个 tomcat 节点中启动的 sess
我开始使用 golang 和 Angular2 构建一个常规的网络应用程序,最重要的是我试图在 auth0.com 的帮助下保护我的登录.我从 here 下载快速入门代码并尝试运行代码,它运行了一段时
我在 Spring Controller 中有一个方法,它接受两个相同类型的参数其中一个来自 session ,另一个来自表单提交(UI)。 问题是在 Controller 方法中我的非 sessio
在我登录之前,我可以点击我的安全约束目录之外的任何内容。如果我尝试转到安全约束目录内的某个位置,它会将我重定向到表单登录页面。如您所料。 登录后,我可以继续我的业务,并访问我的安全约束内外的资源。
我是一名优秀的程序员,十分优秀!