使用 X-Frame-Options 防止被iframe 形成跨域iframe 提交挂掉

 Refused to display 'http://www.***.com/login/doLogin.html' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. 

触发缘由:页面的返回头被设置 X-Frame-Options SAMEORIGIN ,只能被同源的iframe 引用。跨域名的iframe 无法显示了。

解决办法:
第一步 把 服务器上的 X-Frame-Options header 去掉javascript

第二步 添加 以下代码到 不想被iframe 的页面header 里去。
<style id="antiClickjack">body{display:none !important;}</style>
<script>
if (self === top) {
var antiClickjack = document.getElementById("antiClickjack");
antiClickjack.parentNode.removeChild(antiClickjack);
} else {
top.location = self.location;
}
</script>css

其余:

X-Frame-Options ALLOW-FROM 只支持单一域名 想支持多个二级域名的这个无解html

并非全部的浏览器都支持  这个header 因此,低版本的浏览器仍然会被iframe 成功

参考:http://www.css88.com/archives/5141java

Browsers Supporting X-Frame-Options  
http://blogs.msdn.com/b/ieinternals/archive/2010/03/30/combating-clickjacking-with-x-frame-options.aspx

 

没法经过 <meta http-equiv=”X-FRAME-OPTIONS” content=”SAMEORIGIN”> 这种形式在document 的 header 里面设置,只能经过 http header 设置。web

Browsers ignore the header if speicified in the META tag. So the following META will be ignored:跨域

<meta http-equiv="X-Frame-Options" content="deny">

防止被IFRAME :把这些代码放到你的 header 里浏览器

<style id="antiClickjack">body{display:none !important;}</style>
<script>
if (self === top) {
var antiClickjack = document.getElementById("antiClickjack");
antiClickjack.parentNode.removeChild(antiClickjack);
} else {
top.location = self.location;
}
</script>
参考这个帖子
https://www.codemagi.com/blog/post/194服务器

 

关于点击劫持 和 被iframe 的其余参考:post

http://javascript.info/tutorial/clickjacking

http://blogs.msdn.com/b/ieinternals/archive/2010/03/30/combating-clickjacking-with-x-frame-options.aspxui

https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options

带中文请求的URL 可能会返回400 须要 encodeURIComponent 处理.尤为是使用IE 的时候。

相关文章
相关标签/搜索