情况:
我必须连接到这两个不同的服务器以进行开发和登台使用。两台服务器都有不受信任的 SSL 证书。 出于示例目的,这两个服务器 URL 为:
临时服务器:https://52.70.13.2:1010/
开发服务器:https://example.entrydns.org:1012
每当我尝试调用 API 时,都会收到空响应并出现以下错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
或者有时,
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
我使用Moya对于我的网络层,它基本上只是 Alamofire 的一个很好的包装器。 。有关更多信息,我使用 XCode 7.3
并且该应用仅支持 > iOS 9
。
我做了什么:
我非常清楚 Apple 希望强制执行的应用程序传输安全问题。我想禁用它以进行开发,但仍然徒劳。我尝试绕过 ATS 的一些方法如下:
在我的
plist
中添加以下内容以允许任意加载。<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
显式定义域异常(exception)。
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>example.entrydns.org</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSExceptionRequiresForwardSecrecy</key> <true/> <key>NSExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> <false/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <true/> <key>NSThirdPartyExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> <key>NSRequiresCertificateTransparency</key> <false/> </dict> </dict> </dict>
我还尝试禁用 Alamofire 管理器共享实例的服务器信任策略。示例代码如下:
// Disable Policies let policies: [String: ServerTrustPolicy] = [ "https://example.entrydns.org:1012/": .DisableEvaluation, "https://52.70.13.2:1010/": .DisableEvaluation ] let manager = Manager( configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies) ) // --------------- let networkLogger = NetworkLoggerPlugin(verbose: true, responseDataFormatter: nil) let endpointClosure = { (target: ExampleAPI) -> Endpoint<ExampleAPI> in let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString let endpoint: Endpoint<ExampleAPI> = Endpoint<ExampleAPI> (URL: url, sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters, parameterEncoding: .URLEncodedInURL) return endpoint.endpointByAddingHTTPHeaderFields(target.header()) } let ExampleProvider = MoyaProvider<ExampleAPI>(manager: manager, plugins:[networkLogger], endpointClosure: endpointClosure)
打开 URL 并在我的设备和模拟器上下载证书。
即使执行了上述所有步骤,我仍然遇到相同的错误。对我做错了什么以及我能做些什么来解决这个问题有什么看法吗?顺便说一句,如果我可以避免服务器端解决方案,那就太好了。
提前致谢。
引用文献:
最佳答案
仅使用主机名定义您的策略。
// Disable Policies
let policies: [String: ServerTrustPolicy] = [
"example.entrydns.org": .DisableEvaluation
]
关于swift - 绕过应用程序传输安全以允许不安全的 HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38913547/