即使“X-Frame-Options”为“ALLOWALL”,也不能在iframe中显示我的rails 4应用程序

我正在尝试testing一个响应式devise。 我使用的是Rails 4.我知道它将“X-Frame-Options”设置为“SAME ORIGIN”。 所以我在development.rb中使用了它

config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' } 

它的工作。 我在Chrome控制台中查看了networking请求,如下所示:

在这里输入图像说明

但仍然像网站response.is和responsinator.com给我下面的错误:

 Refused to display 'http://localhost:3000/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. about:blank:1 

这是怎么回事??

我遇到了和你一样的问题,并且整夜寻找解决这个问题的办法。

我终于明白了为什么会发生。 这是因为Chromecaching。

你可以看到header['X-Frame-Options']ALLOWALL但不起作用。

只要尝试打开“新隐身窗口”,并进入相同的页面,它的工作原理!

这个问题只发生在我的testing中的开发模式 。 它在生产模式下运行良好。

试着删除这个标题'X-Frame-Options'。 也许这样在控制器中:

 before_filter :allow_iframe_requests ... def allow_iframe_requests response.headers.delete('X-Frame-Options') end 

Rails 4 添加了SAMEORIGIN的默认X-Frame-Options HTTP标头值。 这对安全性很好,但是当你想要在iframe调用你的action ,你可以这样做:


允许所有的起源:

 class MyController < ApplicationController def iframe_action response.headers.delete "X-Frame-Options" render_something end end 

允许特定来源:

 class MyController < ApplicationController def iframe_action response.headers["X-FRAME-OPTIONS"] = "ALLOW-FROM http://some-origin.com" render_something end end 

使用:after_filter

当您需要在iframe使用多个action时,最好制作一个方法并使用:after_filter

 class ApplicationController < ActionController::Base private def allow_iframe response.headers.delete "X-Frame-Options" end end 

像这样在你的控制器中使用它:

 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 

在浏览器中进行硬刷新,或使用其他浏览器查看更改

Via: Rails 4:让特定的动作作为iframeembedded

当使用Heroku和Firefox“加载被X-Frame-Options拒绝”时

我有一个类似的问题,我不断得到这个错误在Firefox。 我有一个托pipe在@Hocha上Rails应用程序的MochaHost PHP网页(所以RoR应用程序有一个指向PHP网页的iframe页面,这个页面可以在所有的浏览器上运行,除了Firefox)。

我能够通过在特定环境文件中为我的所有请求设置默认标题来解决问题:

 # config/enviroments/production.rb config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' } 

编辑 (如sheharyarbuild议)

理想情况下,您不应该设置默认标题,而只需要在iFrame中呈现的操作。 如果您的整个应用程序在iFrame内部提供服务,则应明确提及原点

 # config/enviroments/production.rb config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOW-FROM http://some-origin.com' } 

尝试ALLOW-FROM http://example.com而不是? 如果Chrome有足够新的版本,那么在Chrome中可以使用ALLOWALL [2]

[1] https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options

[2] https://stackoverflow.com/a/16101968/800526

如果您希望在所有环境中使此更改生效,请将其置于application.rb中。

我发现了另一个原因。 假设执行了ALLOWALL或类似的修复程序,下一个问题就是尝试在https网站中使用http内容,这会导致安全风险并被mozilla,IE以及其他浏览器阻止。 我花了6个小时才find这个,希望通过分享我可以减less某些人的痛苦。

可以通过以下方式检查:

  • 使用您的浏览器networking工具应该显示一个错误。
  • networking日志将缺乏与您的供应网站的任何连接。
  • 用银行https主页replace您的内容url应展示iframe否则工作。

解决办法是询问来源是否有https内容或find其他供应商。

参考: