[轉]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 insecure domain, 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、竊聽風險:第三方可以獲知通信內容;
2、篡改風險:第三方可以修改通信內容;
3、冒充風險:第三方可以冒充他人身份參與通信。
SSL/TLS協議是為了解決這三大風險而設計的,希望達到:
1、所有信息都是加密傳播,第三方無法竊聽;
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的作用,可參見:
其他具體配置方法,可參見:
http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/
介於每次iOS系統升級的用戶量都較大,可以預見當iOS9正式版推出后,將會有大批用戶遷移,因而做https適配對於更加的用戶體驗及數據安全都十分重要。
參考:
3、http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html
4、http://www.neglectedpotential.com/2015/06/working-with-apples-application-transport-security/