iOS 允许http请求 关于NSAppTransportSecurity 和 NSExceptionDomains 的关系

当项目设置了 NSExceptionDomains, 不管 NSAllowsArbitraryLoads 是 true/false, NSExceptionDomains 下面 NSExceptionAllowsInsecureHTTPLoads 的值决定了是否允许 http 请求

以阿里云的域名为例, 访问http://aliyuncs.com

以下两种情况允许 http 请求

  1. 允许个别域名 http 请求:
    (1) NSAllowsArbitraryLoads: false,
    (2) NSExceptionDomains内NSExceptionAllowsInsecureHTTPLoads: true
<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<flase/>
		<key>NSExceptionDomains</key>
		<dict>
			<key>aliyuncs.com</key>
			<dict>
				<key>NSExceptionAllowsInsecureHTTPLoads</key>
				<true/>
			</dict>
		</dict>
	</dict>
  1. 允许所有 http 请求:
    (1) NSAllowsArbitraryLoads: true
    (2) 不设置NSExceptionDomains
<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
	</dict>

以下两种情况禁止 http 请求

  1. 禁止个别域名 http 请求:
    (1) NSAllowsArbitraryLoads: true,
    (2) NSExceptionDomains内NSExceptionAllowsInsecureHTTPLoads: false
<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
		<key>NSExceptionDomains</key>
		<dict>
			<key>aliyuncs.com</key>
			<dict>
				<key>NSExceptionAllowsInsecureHTTPLoads</key>
				<false/>
			</dict>
		</dict>
	</dict>
  1. 禁止所有 http 请求:
    (1) NSAllowsArbitraryLoads: false
    (2) 不设置NSExceptionDomains
<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<false/>
	</dict>

总结:
NSExceptionDomains 的作用级别高于 NSAllowsArbitraryLoads
NSExceptionDomains 既可以是白名单, 也可以是黑名单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值