我正在写一个小网页,它的目的是框架其他一些页面,只是为了将它们合并到一个浏览器窗口中,以便于查看。一些页面,我试图框架禁止被框架,并抛出“拒绝显示文档,因为显示禁止X-Frame-Options.”错误在Chrome。我知道这是一个安全限制(有充分的理由),并且无法更改它。

是否有任何替代的框架或非框架方法来在单个窗口中显示页面,而不会被X-Frame-Options报头绊倒?


当前回答

我使用的是Tomcat 8.0.30,没有一个建议对我有效。当我们希望更新X-Frame-Options并将其设置为允许时,以下是我如何配置允许嵌入iframes:

进入Tomcat conf目录,编辑web.xml文件 添加下面的过滤器:

<filter>
            <filter-name>httpHeaderSecurity</filter-name>
            <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
                   <init-param>
                           <param-name>hstsEnabled</param-name>
                           <param-value>true</param-value>
                   </init-param>
                   <init-param>
                           <param-name>antiClickJackingEnabled</param-name>
                           <param-value>true</param-value>
                   </init-param>
                   <init-param>
                           <param-name>antiClickJackingOption</param-name>
                           <param-value>ALLOW-FROM</param-value>
                   </init-param>
            <async-supported>true</async-supported>
       </filter>

       <filter-mapping>
                   <filter-name>httpHeaderSecurity</filter-name>
                   <url-pattern>/*</url-pattern>
                   <dispatcher>REQUEST</dispatcher>
       </filter-mapping> 

重启Tomcat服务 使用iFrame访问资源。

其他回答

<form target="_parent" ... />

根据Kevin Vella的想法,我尝试在PayPal的按钮生成器生成的表单元素上使用上述方法。为我工作,这样贝宝不会在一个新的浏览器窗口/标签打开。

更新

这里有一个例子:

在今天(01-19-2021)生成按钮时,PayPal自动在表单元素上包含target="_top",但如果这对您的上下文不起作用,请尝试不同的目标值。我建议_parent——至少当我使用这个PayPal按钮时,它是有效的。

有关更多信息,请参阅表单目标值。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent">
  <input type="hidden" name="cmd" value="_xclick">
  <input type="hidden" name="business" value="name@email.com">
  <input type="hidden" name="lc" value="US">
  <input type="hidden" name="button_subtype" value="services">
  <input type="hidden" name="no_note" value="0">
  <input type="hidden" name="currency_code" value="USD">
  <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
  <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

将外部网站加载到iFrame的解决方案,即使外部网站的x帧选项设置为拒绝。

如果你想将其他网站加载到iFrame中,而你得到了X-Frame-Options禁止显示的错误,那么你实际上可以通过创建一个服务器端代理脚本来克服这个问题。

iFrame的src属性可以有这样一个url:/ proxy.php?url=https://www.example.com/page&key=somekey

那么proxy.php看起来就像这样:

if (isValidRequest()) {
   echo file_get_contents($_GET['url']);
}

function isValidRequest() {
    return $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['key']) && 
    $_GET['key'] === 'somekey';
}

这通过传递块,因为它只是一个GET请求,可能是一个普通的浏览器页面访问。

注意:您可能需要改进此脚本中的安全性。因为黑客可以通过你的代理脚本开始加载网页。

唯一一个有一堆答案的问题。欢迎来到这个指南,我希望我在最后期限那天晚上10:30为它工作时能有这个指南……facebook在canvas应用上做了一些奇怪的事情,好吧,你已经被警告过了。如果你还在这里,你有一个Rails应用程序将出现在Facebook Canvas后面,那么你将需要:

Gemfile:

gem "rack-facebook-signed-request", :git => 'git://github.com/cmer/rack-facebook-signed-request.git'

配置/ facebook.yml

facebook:
  key: "123123123123"
  secret: "123123123123123123secret12312"

配置/ application.rb

config.middleware.use Rack::Facebook::SignedRequest, app_id: "123123123123", secret: "123123123123123123secret12312", inject_facebook: false

配置/初始化/ omniauth.rb

OmniAuth.config.logger = Rails.logger
SERVICES = YAML.load(File.open("#{::Rails.root}/config/oauth.yml").read)
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, SERVICES['facebook']['key'], SERVICES['facebook']['secret'], iframe:   true
end

application_controller.rb

before_filter :add_xframe
def add_xframe
  headers['X-Frame-Options'] = 'GOFORIT'
end

你需要一个控制器来调用Facebook的画布设置,我使用/canvas/,并使路由到这个应用程序的主SiteController:


class SiteController < ApplicationController
  def index
    @user = User.new
  end
  def canvas
    redirect_to '/auth/failure' if request.params['error'] == 'access_denied'
    url = params['code'] ? "/auth/facebook?signed_request=#{params['signed_request']}&state=canvas" : "/login"
    redirect_to url
  end
  def login
  end
end

login.html.erb


<% content_for :javascript do %>
  var oauth_url = 'https://www.facebook.com/dialog/oauth/';
  oauth_url += '?client_id=471466299609256';
  oauth_url += '&redirect_uri=' + encodeURIComponent('https://apps.facebook.com/wellbeingtracker/');
  oauth_url += '&scope=email,status_update,publish_stream';
console.log(oauth_url);
  top.location.href = oauth_url;
<% end %>

来源

配置我认为来自omniauth的例子。 宝石文件(这是关键!!)来自:slideshare things i learned… 这个堆栈问题有整个Xframe的角度,所以你会得到一个空白,如果 你不需要把这个头文件放到应用控制器中。 我的男人@rafmagana写了这个heroku指南,现在你可以用这个答案来做轨道,也可以用巨人的肩膀走路。

网站所有者使用X-Frame-Options响应报头,这样他们的网站就不能在Iframe中打开。这有助于保护用户免受点击劫持攻击

如果你想在自己的机器上禁用X-Frame-Options,可以尝试几种方法。

服务器端配置

如果您拥有服务器或可以与站点所有者合作,那么您可以要求设置一个配置,以根据某些条件不发送Iframe buster响应标头。条件可以是额外的请求头或URL中的参数。

例如,站点所有者可以添加一个额外的代码,当站点打开时不发送Iframe buster报头,使用?in_debug_mode=true查询参数。

使用浏览器扩展,如Requestly删除响应头

您可以使用任何浏览器扩展,如Requestly,它允许您修改请求和响应头。这里有一个Requestly博客,解释了如何在Iframe中嵌入站点,绕过Iframe buster头。

配置一个直通代理,并从它删除标题

如果你需要为多人绕过Iframe buster报头,那么你也可以配置一个直通代理,它只删除帧buster响应报头并返回响应。然而,这是一个非常复杂的编写和设置。还有一些其他的挑战,比如通过代理在Iframe中打开的站点的身份验证等,但这种方法可以很好地用于简单的站点。

PS -我已经建立了这两个解决方案,并有第一手的经验。

我使用的是Tomcat 8.0.30,没有一个建议对我有效。当我们希望更新X-Frame-Options并将其设置为允许时,以下是我如何配置允许嵌入iframes:

进入Tomcat conf目录,编辑web.xml文件 添加下面的过滤器:

<filter>
            <filter-name>httpHeaderSecurity</filter-name>
            <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
                   <init-param>
                           <param-name>hstsEnabled</param-name>
                           <param-value>true</param-value>
                   </init-param>
                   <init-param>
                           <param-name>antiClickJackingEnabled</param-name>
                           <param-value>true</param-value>
                   </init-param>
                   <init-param>
                           <param-name>antiClickJackingOption</param-name>
                           <param-value>ALLOW-FROM</param-value>
                   </init-param>
            <async-supported>true</async-supported>
       </filter>

       <filter-mapping>
                   <filter-name>httpHeaderSecurity</filter-name>
                   <url-pattern>/*</url-pattern>
                   <dispatcher>REQUEST</dispatcher>
       </filter-mapping> 

重启Tomcat服务 使用iFrame访问资源。