Ste*_*son 42 ruby-on-rails x-frame-options responsive-design
我正在尝试测试响应式设计.我正在使用Rails 4.我知道它将'X-Frame-Options'设置为SAME ORIGIN.所以我在development.rb中使用了
config.action_dispatch.default_headers = {
'X-Frame-Options' => 'ALLOWALL'
}
Run Code Online (Sandbox Code Playgroud)
它起作用了.我在Chrome控制台中查看了网络请求,内容如下:

但仍然像responsive.is和responsinator.com这样的网站给我以下错误:
Refused to display 'http://localhost:3000/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. about:blank:1
Run Code Online (Sandbox Code Playgroud)
这是怎么回事??
Tim*_*ael 67
尝试删除此标题'X-Frame-Options'.也许这种方式在控制器中:
before_filter :allow_iframe_requests
...
def allow_iframe_requests
response.headers.delete('X-Frame-Options')
end
Run Code Online (Sandbox Code Playgroud)
Yi *_*Xie 27
我和你有同样的问题,整夜都在寻找解决这个问题的方法.
我终于找到了它为什么会发生.这是因为Chrome缓存.
你可以看到header['X-Frame-Options']它ALLOWALL但它不起作用.
只需尝试打开"新的隐身窗口"并转到同一页面就可以了!
此问题仅发生在我的测试中的开发模式中.它在生产模式下运行良好.
She*_*yar 22
Rails 4 添加了默认的X-Frame-OptionsHTTP标头值SAMEORIGIN.这是很好的安全性,但是当你不希望你action在一个被称为iframe,你可以这样做:
class MyController < ApplicationController
def iframe_action
response.headers.delete "X-Frame-Options"
render_something
end
end
Run Code Online (Sandbox Code Playgroud)
class MyController < ApplicationController
def iframe_action
response.headers["X-FRAME-OPTIONS"] = "ALLOW-FROM http://some-origin.com"
render_something
end
end
Run Code Online (Sandbox Code Playgroud)
当你需要使用多个actionin中的一个时iframe,最好制作一个方法并用它来调用它:after_filter:
class ApplicationController < ActionController::Base
private
def allow_iframe
response.headers.delete "X-Frame-Options"
end
end
Run Code Online (Sandbox Code Playgroud)
在控制器中使用它,如下所示:
class MyController < ApplicationController
after_filter :allow_iframe, only: [:basic_embed, :awesome_embed]
def basic_embed
render_something
end
def awesome_embed
render_something
end
# Other Actions...
end
Run Code Online (Sandbox Code Playgroud)
在浏览器中执行Hard-Refresh,或使用其他浏览器查看更改
我有一个类似的问题,我只在Firefox上收到此错误.我有一个PHP托管@MochaHost的网页服务Rails托管@ Heroku的应用程序(因此RoR应用程序有一个页面,iframe指向PHP网页,这适用于所有浏览器,除了Firefox).
我能够通过为特定环境文件中的所有请求设置默认标头来解决问题:
# config/enviroments/production.rb
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
Run Code Online (Sandbox Code Playgroud)
编辑 (正如sheharyar建议的那样)
理想情况下,您不应设置默认标头,仅对必须在iFrame中呈现的操作执行此操作.如果你的整个应用程序都在iFrame中提供,你应该明确提到Origin:
# config/enviroments/production.rb
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOW-FROM http://some-origin.com' }
Run Code Online (Sandbox Code Playgroud)