对于swift 3上的自签名证书,此服务器的证书无效

编程入门 行业动态 更新时间:2024-10-24 14:22:17
本文介绍了对于swift 3上的自签名证书,此服务器的证书无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用swift 3并首次点击网络服务。我的网络服务通过HTTPS运行,我想用加密进行测试。

I am using swift 3 and hitting a web service for the first time. My web service runs over HTTPS and I want to test with encryption in place.

这是我到目前为止的代码:

Here's my code so far:

let config = URLSessionConfiguration.default // Session Configuration let session = URLSession(configuration: config) // Load configuration into Session let url = URL(string: webService.getLoginUrl())! let task = session.dataTask(with: url, completionHandler: { (data, response, error) in if error == nil { do { if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{ //Implement your logic print(json) } } catch { print("error in JSONSerialization") } } else { print(error!.localizedDescription) } }) task.resume()

当我对自己签名的测试服务器运行时,我得到:

When I run this against my test server, which is self-signed, I get:

The certificate for this server is invalid. You might be connecting to a server that is pretending to be "10.0.0.51" which could put your confidential information at risk.

所以我想做的是在测试时接受所有证书,但不在生产中。

So what I'd like to do is accept all certificates when testing, but not in production.

我发现了几个网站:

www.byteblocks/Post/Use-self-signed-SSL-certificate -in-iOS-application https: //github/socketio/socket.io-client-swift/issues/326

但这些似乎早于3。

如何解决这个问题?

推荐答案

经过大量研究,我了解到了代理如何使用swift 3中的URLSession对象。发布链接的部分太多,但最后,这是最有帮助的: https:// g ist.github/stinger/420107a71a02995c312036eb7919e9f9

After much research, I learned about how delegates work with URLSession objects in swift 3. Too many pieces to post a link, but in the end, this was the most helpful: gist.github/stinger/420107a71a02995c312036eb7919e9f9

因此,为了解决这个问题,我从URLSessionDelegate继承了我的类,然后添加了以下函数:

So, to fix the problem, I inherited my class from URLSessionDelegate and then added the following function:

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { //accept all certs when testing, perform default handling otherwise if webService.isTesting() { print("Accepting cert as always") completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!)) } else { print("Using default handling") completionHandler(.performDefaultHandling, URLCredential(trust: challenge.protectionSpace.serverTrust!)) } }

isTesting()调用确定我是否正在使用测试服务器,然后如果我们处于测试模式,则接受所有证书。

The isTesting() call determines if I'm using the test server, and then we accept all certificates if we're in testing mode.

更多推荐

对于swift 3上的自签名证书,此服务器的证书无效

本文发布于:2023-11-08 11:34:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   服务器   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!