gpt4 book ai didi

ios - 如何在不将 NSAllowsArbitraryLoads 设置为 yes 的情况下处理自签名证书?

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:07 26 4
gpt4 key购买 nike

我是 swift 的新手,我正在尝试弄清楚如何将一些 POST 请求发送到具有自签名证书的服务器。我现在正在尝试访问开发服务器进行测试。这就是我实现 didReceive 挑战函数的方式:

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let method = challenge.protectionSpace.authenticationMethod
let host = challenge.protectionSpace.host
NSLog("challenge %@ for %@", method, host)
switch (method, host) {
case (NSURLAuthenticationMethodServerTrust, "mydomain.example.com"):
let trust = challenge.protectionSpace.serverTrust!
let credential = URLCredential(trust: trust)
completionHandler(.useCredential, credential)
case (NSURLAuthenticationMethodClientCertificate, "mydomain.example.com"):
completionHandler(.performDefaultHandling, nil)
default:
completionHandler(.cancelAuthenticationChallenge, nil)
}

这就是我的 Info.plist 源使调用正常工作的样子:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>mydomain.example.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>

我一直在研究如何在线执行此操作,这似乎不是最安全的方法 - 将 NSAllowsArbitraryLoads 设置为 true 有哪些风险?我尝试删除该位,但随后出现此错误:

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."

而且我不知道是否有办法在不启用 NSAllowsArbitraryLoads 的情况下执行此操作。对此有何建议?如果仅用于测试,这可以吗?

谢谢!

最佳答案

NSAllowsArbitraryLoads 不应该被使用,即使是在测试中。也许你只是忘记在生产中删除这个参数,所有的安全性都消失了。此外,最好针对具有真实证书的服务器进行测试,以检测可能的错误和安全问题。

如果您没有特殊设置,从Let's Encrypt 获取免费服务器证书应该相对容易。 .这些证书已被 Apple 设备接受,并且一切正常。

另一种可能性是创建您自己的证书颁发机构 (CA) 并将 CA 证书导出到您的设备。然后您的设备将接受此 CA 签署的所有证书。看看this post从我这里获取详细信息。

关于ios - 如何在不将 NSAllowsArbitraryLoads 设置为 yes 的情况下处理自签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com