微信掃碼登陸在chrome瀏覽器失敗,瀏覽器禁止重定向


瀏覽器會提示:(新版本的瀏覽器會提示)
Unsafe JavaScript attempt to initiate navigation for frame with URL ‘http://www.xxx.xxx/’ from frame with URL “https://open.weixin.com/xxxxxxx” The frame attempting navigation is targeting its top-level window, but is neither same-origin with its target nor is it processing a user gesture

大概意思是:瀏覽器監測到了iframe中存在不安全的鏈接正在嘗試進行導航,
1. 再來看微信掃碼登陸的js文件:wxLogin.js
2. 該文件中對與iframe處理上述安全問題的屬性sendbox是沒有添加的
3. iframe默認情況下:

  在html5頁面中,可以使用iframe的sandbox屬性,<iframe src="http://alibaba.com" sandbox>sandbox后面如果不加任何值,就代表采用默認的安全策略,

  即:iframe的頁面將會被當做一個獨自的源,同時不能提交表單,以及執行javascript腳本,也不能讓包含iframe的父頁面導航到其他地方,所有的插件,如flash,applet等也全部不能起作用。

  簡單說iframe就只剩下一個展示的功能,正如他的名字一樣,所有的內容都被放入了一個單獨的沙盒。

.
4. sendbox包含的屬性及作用:

allow-forms 允許進行提交表單
allow-scripts 運行執行腳本
allow-same-origin 允許同域請求,比如ajax,storage
allow-top-navigation 允許iframe能夠主導window.top進行頁面跳轉
allow-popups 允許iframe中彈出新窗口,比如,window.open,target=”_blank”
allow-pointer-lock 在iframe中可以鎖定鼠標,主要和鼠標鎖定有關

 

5. 修改wxLogin.js如下:

d.sandbox = "allow-scripts allow-top-navigation allow-same-origin",

6.wxLogin.js修改后完整代碼

!function (a, b, c) {
     function d(a) { 
         var c = "default"; 
         a.self_redirect === !0 ? c = "true" : a.self_redirect === !1 && (c = "false");
         var d = b.createElement("iframe"),
          e = "https://open.weixin.qq.com/connect/qrconnect?appid=" + a.appid + "&scope=" + a.scope + "&redirect_uri=" + a.redirect_uri + "&state=" + a.state + "&login_type=jssdk&self_redirect=" + c + '&styletype=' + (a.styletype || '') + '&sizetype=' + (a.sizetype || '') + '&bgcolor=' + (a.bgcolor || '') + '&rst=' + (a.rst || '');
          e += a.style ? "&style=" + a.style : "",
          e += a.href ? "&href=" + a.href : "",
          d.src = e, 
          d.frameBorder = "0", 
          d.allowTransparency = "true", 
          d.sandbox = "allow-scripts allow-top-navigation allow-same-origin", 
          d.scrolling = "no", 
          d.width = "300px", 
          d.height = "400px"; 
         var f = b.getElementById(a.id);
         f.innerHTML = "", 
         f.appendChild(d) 
    }
    a.WxLogin = d 
}(window, document);

 

————————————————
版權聲明:本文為CSDN博主「yangdong_MC」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sinat_36193631/article/details/82344663


免責聲明!

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



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