Help - preventing browser caching by setting headers in go not working

xuanbao · · 1066 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Did not get any answers in SO so hoping someone here can help:</p> <p>I&#39;m trying to prevent users from going back to the login page after logging in by accessing their browser cache.</p> <p>Here is my handler for the login page in main:</p> <p><em>http.HandleFunc(&#34;/login&#34;, login):</em></p> <p>After login the user gets directed to index.html and the handler is also in main:</p> <p><em>http.HandleFunc(&#34;/&#34;, serveHtml)</em></p> <p>in my login function I&#39;ve tried to set the headers to prevent caching:</p> <blockquote> <p>func login(w http.ResponseWriter, r *http.Request) {</p> <pre><code> w.Header().Set(&#34;Cache-Control&#34;, &#34;no-cache, private, max-age=0&#34;) w.Header().Set(&#34;Expires&#34;, time.Unix(0, 0).Format(http.TimeFormat)) w.Header().Set(&#34;Pragma&#34;, &#34;no-cache&#34;) w.Header().Set(&#34;X-Accel-Expires&#34;, &#34;0&#34;) //... the rest of my login code here. Upon successful login redirects to &#34;/&#34; </code></pre> <p>}</p> </blockquote> <p>There are no problems when the /login page is loaded again (either through a logout redirect or if for some reason the user intentionally visits the login page). I expire the Cookie and everything behaves normally.</p> <p>But I&#39;m trying to prevent the user from hitting the Back button after login and accessing their browser cache to get back to the login page. I thought setting the headers in my login function as suggested by some other answers on SO would prevent the browser from caching the login page.</p> <p>But that doesn&#39;t work and the user can still hit the Back button and access the login page from their browser cache after logging in.</p> <p>How do I set the headers correctly to prevent browser caching of the login page?</p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1066 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传