Rails(set_no_cache方法)无法禁用Safari和Opera中的浏览器缓存

问题描述 投票:0回答:3

使用 Devise 进行身份验证后,我发现存在一个安全漏洞,即用户注销后,会话变量会被保留。这允许任何人按后退按钮并访问登录用户的上一个屏幕。

我看了这些帖子 数字1 数字2 数字3

我将这些行添加到我的 application_controller 中

before_filter :set_no_cache
def set_no_cache
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

在 _form.html.erb 中我将其添加到顶部

<%if user_signed_in? %>
<%=link_to "Sign Out",  destroy_user_session_path, :method => :delete %><br/>
<%= form_for(@listing) do |f| %>
<% if @listing.errors.any? %>
...........

然后我在 Firefox、Chrome 和 Safari 上测试了该应用程序。

Firefox 和 Chrome 没有问题,因为我注销并点击后退按钮,无法看到用户之前的屏幕,但是,在 Safari 和 Opera 中,不安全行为仍然存在。这段代码没有效果。

关于如何解决这个问题有什么建议吗?

谢谢

ruby-on-rails ruby safari opera browser-cache
3个回答
13
投票

我遇到了同样的问题并找到了一个很好的解决方案

要添加“无缓存”,请在 application_controller.rb 文件中添加以下行

before_filter :set_no_cache

和功能

def set_no_cache
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

1
投票

首先,对于任何缓存问题,请使用 Mark Nottingham 的 HTTP 缓存指南

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

试试这个。


0
投票

我发现在我的应用程序控制器中执行此操作非常适合开发。

after_filter  :expire_for_development

protected

def expire_for_development
  expires_now if Rails.env.development?
end
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.