即使'X-Frame-Options'是'ALLOWALL',也无法在iframe中显示我的rails 4 app

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)

  • 您可以使用它来为整个应用程序应用此更改> http://stackoverflow.com/questions/16561066/ruby-on-rails-4-app-not-works-in-iframe (4认同)
  • 这是正确答案,因为`ALLOWALL`不是`X-Frame-Options`(https://tools.ietf.org/html/rfc7034)的有效值.缺少标题将使浏览器"允许所有"起源 (2认同)

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)

使用:after_filter

当你需要使用多个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,或使用其他浏览器查看更改

Via:Rails 4:让特定的动作嵌入为iframe

  • 对ALLOW-FROM的支持并不广泛:在Chrome或Safari中无法使用.请参阅http://stackoverflow.com/questions/10658435/x-frame-options-allow-from-in-firefox-and-chrome和https://developer.mozilla.org/en-US/docs/Web/HTTP/X-框架 - 选项#Browser_compatibility (4认同)

d1j*_*i1b 9

当使用Heroku和Firefox"通过X-Frame-Options拒绝加载"时

我有一个类似的问题,我在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)