更新 cookie 时缓解竞争条件的策略?
因此,情况是,在给定页面上有许多 HTTP 请求,每个请求都需要更新单个共享 cookie。无法控制每页发出的请求数量,但实际上可能有 10 个以上。
我们已经看到只有最后一个 http 请求成功更新 cookie 的问题,我被告知这是由于浏览器依赖性造成的。
这是一个普遍已知的问题吗?是否有任何降低风险的策略?
So the situation is that on a given page there are many HTTP requests and each of these need to update a single shared cookie. There is no possibility to control how many requests are made per page, but realistically there can be 10+.
We've seen issues where only the last http request successfully updates the cookie and I am told this is due to browser dependencies.
Is this a known issue in general and are there any strategies for mitigating the risk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种策略是避免在 cookie 本身中存储可变信息,而是让 cookie 成为某种标识符,用于标识某种数据库中某处的记录。然后,您的服务器更新(服务器端)数据库中的信息,而不是更改 cookie 值。
One strategy is to avoid storing mutable information in the cookie itself, and instead have the cookie be an identifier of some sort that identifies a record in a database of some kind somewhere. Then, your server updates information in the (server-side) database, instead of changing the cookie value.
这些是什么样的请求?唯一无法控制顺序的是图像、脚本 src 和 iframe 等。并且您不应该更新其中任何一个的 cookie。
您应该在主页上更新 Cook,即带有 HTML 源代码的页面。之后,如果您有 ajax 请求,您就可以控制订单,所以您应该不会遇到问题。
What kind of requests are these? The only ones where you don't control the order are things like images, script src, and iframe. And you should not be updating cookies for any of those.
You should be updating the cook in the main page, the one with the HTML source. After that if you have ajax requests you are in control of the order so you should not have a problem there.