处理应用程序传输安全(kCFStreamErrorDomainSSL,-9802)

Handling App Transport Security (kCFStreamErrorDomainSSL, -9802)

你运行这个代码:

let URL = "https://www.nasa.gov/sites/default/files/wave_earth_mosaic_3.jpg"
let imageData = NSData(contentsOfURL: NSURL(string: URL)!)
UIImage(data: imageData!)

你会得到这个:

2015-09-11 16:33:47.433 Cassini[21200:447896] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

深入挖掘会发现使用了 SHA1 签名。

maximveksler$ openssl s_client -connect www.nasa.gov:443 < /dev/null 2>/dev/null | openssl x509 -text -in /dev/stdin | grep "Signature Algorithm"
    Signature Algorithm: sha1WithRSAEncryption
    Signature Algorithm: sha1WithRSAEncryption

自 2015 年 9 月 11 日起,NASA 正在使用不安全的连接,现在怎么办?

为什么会这样?

因为使用不安全的网络不利于您的用户隐私。

从 iOS9 开始,Apple 将强制您的应用程序与通过 HTTP 访问的任何资源建立安全连接。这意味着您要连接的服务器需要跟进最新的安全连接最佳实践。

截至 2015 年 9 月,这些包括:

可以在 App Transport Security Technote

找到更多信息

你能做什么?

管理自己的服务器?修理它!确保它们坚固且安全。您可以通过 shaaaaaaaaaaaaa.com or locally with any of the methods outline here

在线测试来验证您的服务器是否正常

如果您要连接到 ,可以选择 "white list" 有问题的资源,不鼓励这样做。

降低特定 URL

的安全性

转到您的 Info.plist 并添加以下条目:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>www.nasa.gov</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

你的 plist 应该是这样的:

全局关闭 App Transport Security

请注意,这是一个非常糟糕的主意。

转到您的 Info.plist 并添加以下条目:

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

你的 plist 应该是这样的: