swi*_*Boy 22 ssl-certificate ios nsurlsession swift alamofire
我收到自签名证书的错误
错误域= NSURLErrorDomain代码= -1200"发生SSL错误,无法与服务器建立安全连接.
同时测试我的一个演示应用程序的Web服务
注意: 在假设其重复之前,我会请求请一直阅读,即使我已向苹果开发论坛报告过
使用Alamofire图书馆
func testAlamofireGETRequest() -> Void
{
Alamofire.request(.GET, "https://filename.hostname.net/HelloWeb/service/greeting/john")
.responseJSON
{ response in
print("Response JSON: \(response.result.value)")
}
}
Run Code Online (Sandbox Code Playgroud)
使用NSURLSession
func testNSURLSessionRequest() -> Void {
let session = NSURLSession.sharedSession()
let urlString = "https://filename.hostname.net/HelloWeb/service/greeting/john"
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
print("done, error: \(error)")
//Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made.
}
dataTask.resume()
}
Run Code Online (Sandbox Code Playgroud)
我花了2天没运气:(
已经发布了一堆问题,但对我没什么用
发布了Alamofire git问题
我的Info.pist文件以这种方式更新为ATS设置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>filename.hostname.net</key>
<dict>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
同时我能得到回应
HTTP://filename.hostname.net
和HTTPS://google.com
但不是HTTPS://filename.hostname.net
任何人都可以建议我为什么在经过巨大努力后我无法完成这项工作?
yaa*_*kov 42
在OS X的命令行中,运行以下命令:
nscurl --ats-diagnostics https://filename.hostname.net --verbose
这将告诉您什么样的ATS设置组合将允许iOS访问您的网站,并且应该指出您的网站有什么问题.
它可以是以下一种或多种
Apple已发布App Transport Security的完整要求列表.
原来我们正在使用TLS v1.2但是缺少其他一些要求.
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
我认为您尝试连接的服务器证书无效或与ECC,Ciphers等的iOS 9标准不匹配。
如果您使用的是高级网络API(NSURLSession,NSURLConnection或位于它们之上的任何东西),则无法直接控制客户端提供的密码套件。这些API使用它们自己的内部逻辑选择一组密码套件。
如果您使用的是较低级的网络API(CFSocketStream,通过其NSStream和CFStream API,以及其他更低级别的API),则可以显式选择要使用的一组密码套件。如何执行此操作取决于特定的API。
标准做法是:
创建流对
为TLS配置它
使用kCFStreamPropertySSLContext属性获取安全传输上下文
在该上下文中配置特定属性
打开溪流
您可以在TLSTool示例代码中看到一个示例。具体来说,请查看TLSToolServer类,您可以在其中确切地看到此序列。
在非常短的上下文中,您想要以一种绕过安全性的方式配置流,但是,对于Alamofire,您可以通过以下方法直接执行此操作:
func bypassAuthentication() {
let manager = Alamofire.Manager.sharedInstance
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
var credential: NSURLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = NSURLSessionAuthChallengeDisposition.UseCredential
credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .CancelAuthenticationChallenge
} else {
credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
if credential != nil {
disposition = .UseCredential
}
}
}
return (disposition, credential)
}
}
Run Code Online (Sandbox Code Playgroud)
让我知道是否有帮助。谢谢!
归档时间: |
|
查看次数: |
19222 次 |
最近记录: |