使用 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 去掉

 

第二步 添加 如下代碼到 不想被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>

 

其他:

X-Frame-Options ALLOW-FROM 只支持單一域名 想支持多個二級域名的這個無解

 

並不是所有的瀏覽器都支持  這個header 所以,低版本的瀏覽器仍然會被iframe 成功

參考:http://www.css88.com/archives/5141

 

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 設置。

 

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 的其他參考:

 

http://javascript.info/tutorial/clickjacking

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

 

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

 

帶中文請求的URL 可能會返回400 需要 encodeURIComponent 處理.尤其是使用IE 的時候。

 

轉自:http://www.cnblogs.com/trance/p/4645486.html

 更多參考:http://www.cnblogs.com/xuanhun/p/3610981.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM