猿问

如何在所有浏览器中控制网页缓存?

我们的调查向我们表明,并非所有浏览器都以统一的方式尊重http缓存指令。

出于安全原因,我们不希望在我们的应用程序某些网页缓存,有史以来,通过Web浏览器。这必须至少适用于以下浏览器:

  • Internet Explorer 6+

  • Firefox 1.5+

  • Safari 3+

  • Opera 9+

我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮查看缓存页面。如何在所有浏览器中控制网页缓存?


慕容3067478
浏览 955回答 4
4回答

慕妹3242003

首先,后退按钮历史记录不是缓存:新鲜度模型(第4.2节)不一定适用于历史机制。也就是说,历史机制即使已经过期也可以显示先前的表示。在旧的HTTP规范中,措辞甚至更强,明确告诉浏览器忽略后退按钮历史记录的缓存指令。回到应该回到过去(到时候用户在登录)。它不会向前导航到先前打开的URL。但是,实际上,在非常特定的情况下,缓存可以影响后退按钮:页面必须通过HTTPS提供,否则此缓存破坏将不可靠。另外,如果您不使用HTTPS,那么您的页面很容易受到许多其他方式的登录窃取。你必须发送Cache-Control: no-store, must-revalidate(一些浏览器观察no-store和一些观察must-revalidate)你永远不需要任何:<meta>使用缓存标头 - 它根本不起作用。完全没用。post-check/&nbsp;pre-check- 它是仅适用于可缓存资源的IE-only指令。两次或十几个部分发送相同的标题。一些PHP片段实际上取代了以前的标题,导致只发送了最后一个。如果需要,可以添加:no-cache或者max-age=0,这将使资源(URL)“陈旧”,并要求浏览器检查服务器是否有更新的版本(no-store已经暗示这更强)。ExpiresHTTP / 1.0客户端的过去日期(尽管这些天真正的&nbsp;HTTP / 1.0仅客户端完全不存在)。

桃花长相依

正如porneL所述,您想要的不是停用缓存,而是停用历史缓冲区。不同的浏览器有自己的微妙方法来禁用历史缓冲区。在Chrome(v28.0.1500.95 m)中,我们只能这样做Cache-Control: no-store。在FireFox(v23.0.1)中,其中任何一个都可以工作:Cache-Control: no-storeCache-Control: no-cache&nbsp;(仅限https)Pragma: no-cache&nbsp;(仅限https)Vary: *&nbsp;(仅限https)在Opera(v12.15)中,我们只能通过Cache-Control: must-revalidate(仅限https)执行此操作。在Safari(v5.1.7,7534.57.2)中,任何一个都可以工作:Cache-Control: no-store&nbsp;<body onunload="">&nbsp;在HTML中Cache-Control: no-store&nbsp;(仅限https)在IE8(v8.0.6001.18702IC)中,任何一个都可以工作:Cache-Control: must-revalidate, max-age=0Cache-Control: no-cacheCache-Control: no-storeCache-Control: must-revalidate&nbsp;Expires: 0Cache-Control: must-revalidate&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMTPragma: no-cache&nbsp;(仅限https)Vary: *&nbsp;(仅限https)结合上述内容为我们提供了适用于Chrome 28,FireFox 23,IE8,Safari 5.1.7和Opera 12.15的解决方案:(&nbsp;Cache-Control: no-store, must-revalidate仅限https)请注意,需要https,因为Opera不会为普通的http页面停用历史记录缓冲区。如果你真的无法获得https并且你准备忽略Opera,你可以做的最好的是:Cache-Control:&nbsp;no-store<body&nbsp;onunload="">下面显示了我的测试的原始日志:HTTP:Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Safari 5.1.7,Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Safari 5.1.7,Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: no-store&nbsp;失败:Safari 5.1.7,Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8Cache-Control: no-store&nbsp;<body onunload="">&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: no-cache&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Vary: *&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15&nbsp;成功:无Pragma: no-cache&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15&nbsp;成功:无Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: must-revalidate, max-age=0&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: must-revalidate&nbsp;Expires: 0&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: must-revalidate&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15&nbsp;成功:IE8Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15&nbsp;成功:无HTTPS:Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15&nbsp;成功:无Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15&nbsp;成功:无Vary: *&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Pragma: no-cache&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: no-cache&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: must-revalidate&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7&nbsp;成功:Opera 12.15Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7&nbsp;成功:Opera 12.15Cache-Control: must-revalidate, max-age=0&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7&nbsp;成功:IE8,Opera 12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,Safari 5.1.7&nbsp;成功:FireFox 23,IE8,Opera 12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Chrome 28,Safari 5.1.7&nbsp;成功:FireFox 23,IE8,Opera 12.15Cache-Control: no-store&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;<body onunload="">&nbsp;失败:Opera 12.15&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Chrome 28,Safari 5.1.7,Opera 12.15&nbsp;成功:FireFox 23,IE8Cache-Control: must-revalidate&nbsp;Expires: 0&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,&nbsp;成功:IE8,Opera 12.15Cache-Control: must-revalidate&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,&nbsp;成功:IE8,Opera 12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: 0&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,&nbsp;成功:IE8,Opera 12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;<body onunload="">&nbsp;失败:Chrome 28,FireFox 23,Safari 5.1.7,&nbsp;成功:IE8,Opera 12.15Cache-Control: private, must-revalidate&nbsp;Expires: Sat, 12 Oct 1991 05:00:00 GMT&nbsp;Pragma: no-cache&nbsp;Vary: *&nbsp;失败:Chrome 28,Safari 5.1.7&nbsp;成功:FireFox 23,IE8,Opera 12.15Cache-Control: no-store, must-revalidate&nbsp;失败:无&nbsp;成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15

波斯汪

我发现web.config路由很有用(尝试将其添加到答案中但似乎没有被接受,因此在此处发布)<configuration><system.webServer> &nbsp;&nbsp;&nbsp;&nbsp;<httpProtocol> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<customHeaders> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<add&nbsp;name="Cache-Control"&nbsp;value="no-cache,&nbsp;no-store,&nbsp;must-revalidate"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;HTTP&nbsp;1.1.&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<add&nbsp;name="Pragma"&nbsp;value="no-cache"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;HTTP&nbsp;1.0.&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<add&nbsp;name="Expires"&nbsp;value="0"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Proxies.&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</customHeaders> &nbsp;&nbsp;&nbsp;&nbsp;</httpProtocol></system.webServer>这是express / node.js做同样的方式:app.use(function(req,&nbsp;res,&nbsp;next)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;res.setHeader('Cache-Control',&nbsp;'no-cache,&nbsp;no-store,&nbsp;must-revalidate'); &nbsp;&nbsp;&nbsp;&nbsp;res.setHeader('Pragma',&nbsp;'no-cache'); &nbsp;&nbsp;&nbsp;&nbsp;res.setHeader('Expires',&nbsp;'0'); &nbsp;&nbsp;&nbsp;&nbsp;next();});
随时随地看视频慕课网APP
我要回答