iOS9 new_001:iOS9网络适配(ATS)

[转]iOS9 new_001:iOS9网络适配(ATS)

下载Xcode7打开APP后大家都发现自己的APP无法联网了,why?

苹果官方文档介绍如下:

App Transport Security

App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn‘t follow this requirement, an error is thrown. If your app needs to make a request to an insecuredomain, you have to specify this domain in your app‘s Info.plist file

即,从iOS9开始,所有的http请求都改成了https,采用TLS 1.2协议,目的是增强数据安全。如果不更新的话,暂时可以在Info.plist中声明,使用不安全的网络请求。

先介绍一下设置方法:

最简单的方法在Info.plist中添加以下字段(ATL disabled):

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

也即:

技术分享

下面详细讨论下ATS:

什么是TLS?

TLS(传输层安全协议)是在SSL 3.0基础上发展起来的,应用最广泛的是TLS 1.0,接下来是SSL 3.0,但主流浏览器都已经实现了TLS 1.2的支持。TLS 1.0通常被标示为SSL 3.1,TLS 1.1为SSL3.2,TLS 1.2为SSL 3.3。

TLS/SSL的作用是对传输的信息加密并签名,保证第三方无法获得被解密的信息和伪造其中一方向对方发送信息。

其基本思路是采用公钥加密法,即客户端先向服务器端索要公钥,然后用公钥加密信息,服务器收到密文后,用自己的私钥解密。

具体的实现细节和运行过程在此不做赘述。

TLS和https有什么关系?

https说白了就是http协议和SSL/TLS协议的组合,可以大致理解为“HTTP over SSL”或“HTTP over TLS”。

不使用SSL/TLS的HTTP通信,所有信息明文传播,带来了三大风险:

1、qie ting风险:第三方可以获知通信内容;

2、篡改风险:第三方可以修改通信内容;

3、冒充风险:第三方可以冒充他人身份参与通信。

SSL/TLS协议是为了解决这三大风险而设计的,希望达到:

1、所有信息都是加密传播,第三方无法qie ting;

2、具有校验机制,一旦被篡改,通信双方会立刻发现;

3、配备身份证书,防止身份被冒充。

众所周知,iOS9加强了关于用户隐私和数据安全的保护,故强制升级为https,并将其称为App Transport Security(ATS)。虽然任性,但也是符合苹果作风的合理决定。

我们要做些什么?(适配解决方案)

简单来说,两种选择:

选择一(也是根本选择):服务端升级为TLS 1.2,解析数据。

选择二(解决燃眉之急):在Info.plist中进行适配。

下面详细介绍一下选择二的配置。

最简单的适配方法是在Info.plist配置中改为下面的XML源码:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

也即:

技术分享


事实上,除了NSAllowsArbitraryLoads外,还有很多其他的key,如下:

技术分享技术分享

其含义分别为:

NSAppTransportSecurity

ATS的top level key.

NSAllowsArbitraryLoads

设置是否对任意请求关闭ATS,默认为NO(开启)。

NSExceptionDomains

放置需要关闭ATS的domains的字典。

<domain-name-for-exception-as-string>

设置需要关闭ATS的domains。其中key为域名。

NSExceptionMinimumTLSVersion

设置支持的最低TLS版本,默认为TLS 1.2.

NSExceptionRequiresForwardSecrecy

设置使用的密码方式。

NSExceptionAllowsInsecureHTTPLoads

设置是否允许http请求,默认为NO。

NSRequiresCertificateTransparency

设置是否需要SSL证书。

NSIncludesSubdomains

设置是否允许subdomains

NSThirdPartyExceptionMinimumTLSVersion

NSThirdPartyExceptionRequiresForwardSecrecy

NSThirdPartyExceptionAllowsInsecureHTTPLoads

设置第三方的ATS。


具体每个key的作用,可参见:

https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

其他具体配置方法,可参见:

http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/

介于每次iOS系统升级的用户量都较大,可以预见当iOS9正式版推出后,将会有大批用户迁移,因而做https适配对于更加的用户体验及数据安全都十分重要。

参考:

1、https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9.html#//apple_ref/doc/uid/TP40016198-DontLinkElementID_13

2、https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

3、http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html

4、http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/

猜你喜欢

转载自cookices.iteye.com/blog/2260718