App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure 解决

本文介绍如何解决Xcode7升级后使用AFNetworking插件出现的警告问题。需在Info.plist文件中添加NSAppTransportSecurity及NSAllowsArbitraryLoads配置,允许非安全连接。

做项目的时候经常会用到网络请求,因此AFNetworking这个插件可谓是非常好用,但是在升级之后,再用AFNetworking会报如下警告,表示不能对网络进行请求:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

上网查了一下,说是因为xcode,说是因为AFNetworking访问的是https://,但是升级之后,Xcode默认的访问是http://,因此需要在info.list里面进行如下操作:


首先找到Supporting Files->Info.plist



然后在里面添加一个key:NSAppTransportSecurity,类型为字典类型(Xcode7之后会自动变为:App Transport Security Settings)





接着在上一个字典里面再添加一个key:NSAllowsArbitraryLoads  类型为Boolean 值设置为YES(Xcode7之后会自动变为:Allow Arbitrary Loads)



通过这样设置,那么AFNetworking的插件就可以运行在我们的Xcode7.0的版本了

这是一个常见的浏览器安全错误,通常出现在你尝试从一个网页访问另一个不同源(origin)的 iframe 或 window 的时候。浏览器通过 **同源策略(Same-Origin Policy)** 来阻止这种跨域访问,以防止恶意网站窃取敏感信息。 --- ### 错误信息解释: ``` DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame. ``` - 你当前的网页运行在 `http://localhost:8080` - 你试图访问另一个不同源的 frame(例如嵌套的 iframe 或者通过 `window.open` 打开的窗口) - 由于两个页面的 origin(协议 + 域名 + 端口)不同,浏览器阻止了你的访问 --- ## ✅ 解决方案 ### ✅ 1. **确保同源** 最简单、最安全的方法是确保你要访问的 frame 与主页面是 **同源** 的。 ```html <!-- 确保 src 的 origin 与当前页面一致 --> <iframe id="myIframe" src="http://localhost:8080/other-page.html"></iframe> ``` ### ✅ 2. **使用 postMessage 进行跨域通信** 如果你确实需要跨域访问,可以使用 HTML5 的 `postMessage()` API 来实现安全的跨域通信。 #### 主页面代码(http://localhost:8080): ```html <iframe id="myIframe" src="http://example.com/iframe.html"></iframe> <script> const iframe = document.getElementById('myIframe'); // 向 iframe 发送消息 iframe.contentWindow.postMessage('Hello from localhost', 'http://example.com'); // 监听来自 iframe 的响应 window.addEventListener('message', function(event) { if (event.origin !== 'http://example.com') return; // 安全检查 console.log('收到消息:', event.data); }); </script> ``` #### iframe 页面代码(http://example.com/iframe.html): ```html <script> // 监听主页面发来的消息 window.addEventListener('message', function(event) { if (event.origin !== 'http://localhost:8080') return; console.log('收到主页面消息:', event.data); // 回复消息 event.source.postMessage('Hello from iframe', event.origin); }); </script> ``` ### ✅ 3. **服务器设置 CORS 头(适用于 API 通信)** 如果你是通过 iframe 页面请求 API 数据,而不是直接访问 DOM,可以在服务器端添加 CORS 头: ```http Access-Control-Allow-Origin: * ``` 或者更安全地指定允许的源: ```http Access-Control-Allow-Origin: http://localhost:8080 ``` --- ## ❌ 不推荐的做法(不安全) ### 禁用浏览器安全策略(仅限开发测试) 你可以通过启动浏览器时加上参数来禁用同源策略,**仅用于本地调试**,**不要用于生产环境**: ```bash # Chrome 示例 chrome.exe --disable-web-security --user-data-dir="C:/ChromeDevSession" ``` --- ## 🔍 常见问题排查 | 问题类型 | 检查点 | |----------|--------| | 不同域名 | `localhost` vs `127.0.0.1` | | 不同端口 | `localhost:8080` vs `localhost:3000` | | 不同协议 | `http` vs `https` | | 子域名 | `a.example.com` vs `b.example.com` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值