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

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


当前回答

网站所有者使用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 -我已经建立了这两个解决方案,并有第一手的经验。

其他回答

I came across this issue when running a wordpress web site. I tried all sorts of things to fix it and wasn't sure how, ultimately the issue was because I was using DNS forwarding with masking, and the links to external sites were not being addressed properly. i.e. my site was hosted at http://123.456.789/index.html but was masked to run at http://somewebSite.com/index.html. When i entered http://123.456.789/index.html in the browser clicking on those same links resulted in no X-frame-origins issues in the JS console, but running http://somewebSite.com/index.html did. In order to properly mask you must add your host's DNS name servers to your domain service, i.e. godaddy.com should have name servers of example, ns1.digitalocean.com, ns2.digitalocean.com, ns3.digitalocean.com, if you were using digitalocean.com as your hosting service.

虽然没有提及,但在某些情况下可以有所帮助:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState !== 4) return;
    if (xhr.status === 200) {
        var doc = iframe.contentWindow.document;
        doc.open();
        doc.write(xhr.responseText);
        doc.close();
    }
}
xhr.open('GET', url, true);
xhr.send(null);

试试这个东西,我认为没有人在主题中建议过这个,这将解决你70%的问题,对于其他一些页面,你必须废弃,我有完整的解决方案,但不是公开的。

添加到下面的iframe中

沙箱="允许-同源允许-脚本允许-弹出窗口允许-表单"

如果在尝试嵌入Vimeo内容时遇到此错误,请更改iframe的src,从:https://vimeo.com/63534746更改为:http://player.vimeo.com/video/63534746

我使用的是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访问资源。