关于防止 iframe 嵌套

网页被他人嵌套,可能会引发一些问题:

大量的403/404 访问问题,

可能会被木马网站劫持,

地址无法与内容对应。

 

面对这样的情况,处理的方法有以下几种

1、在haed中设置mata属性

<meta http-equiv="X-Frame-Options" content="SAMEORIGIN">
或者更详细:

<meta http-equiv="X-Frame-Options" content="SAMEORIGIN / DENY "> 

指定具体网站:
<meta http-equiv="X-Frame-Options" content="ALLOW-FROM http://xxx.xxx.com">

 

2、使用JS代码控制

if (top.location != self.location){
    alert("本页面被其他网站嵌套");
    // 具体操作....
    top.location=self.location
}

或者: (function(window,document){ if(top != window){ top.location = location.href; } document.uniqueID != document.uniqueID && !!location.hash && (location.hash = location.hash); window.focus(); })(this,document);

 

 

3、服务器中设置拦截

配置 Apache 在所有页面上发送 X-Frame-Options 响应头,需要把下面这行添加到 'site' 的配置中:

Header always append X-Frame-Options SAMEORIGIN

配置 nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

add_header X-Frame-Options SAMEORIGIN

配置 IIS 发送 X-Frame-Options 响应头,添加下面的配置到 Web.config 文件中:

<system.webServer>
  ...
 
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="SAMEORIGIN" />
    </customHeaders>
  </httpProtocol>
 
  ...
</system.webServer>

 

在web.xml文件下添加以下内容:

<!-- 设置Frame头,防止被嵌套 --> 
<filter>
 <filter-name>FrameFilter</filter-name> 
<filter-class>filter.FrameTao</filter-class>
 </filter> 
<filter-mapping>
 <filter-name>FrameFilter</filter-name>
 <url-pattern>/*</url-pattern> 
</filter-mapping>

 

posted @ 2021-02-23 14:41  我真的看不懂  阅读(1266)  评论(0编辑  收藏  举报