【问题标题】:How Can I Access Local Https with invalid certificate With Alamofire?如何使用 Alamofire 访问具有无效证书的本地 Https?
【发布时间】:2017-05-23 23:54:16
【问题描述】:

我正在尝试使用 Alamofire 连接到本地网络上的 Web 服务器。如果我用浏览器打开 url,它会给出一个 ssl 警告。如果我只是忽略并将异常添加到浏览器,它肯定有效。
我阅读了关于这个主题的不同帖子,包括 Alamofire Github 页面,但我无法让它工作。我做错了什么?
提前致谢!
这是测试代码:
我在 ViewController 类下面的代码开头声明了管理器。

let manager = SessionManager(configuration: URLSessionConfiguration.default, serverTrustPolicyManager: ServerTrustPolicyManager(policies:["10.0.1.19:4491":.disableEvaluation]))
let parameters:Parameters = ["macro":"696B38D0-AF57-4991-83DD-DFD03F1A693B", "value":""]
manager.request("https://10.0.1.19:4491/authenticatedaction.html", parameters:parameters).authenticate(user:user, password:password).response {
 response in
  debugPrint(response)
}

这里是错误:

Alamofire.DefaultDataResponse(request: Optional(https://10.0.1.19:4491/authenticatedaction.html?macro=696B38D0-AF57-4991-83DD-DFD03F1A693B&value=), response: nil, data: Optional(0 bytes), error: Optional(Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “10.0.1.19†which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x170134a00>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=(
    "<cert(0x108066e00) s: Keyboard Maestro i: Keyboard Maestro CA>"
), NSUnderlyingError=0x170259d70 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x170134a00>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, kCFStreamPropertySSLPeerCertificates=(
    "<cert(0x108066e00) s: Keyboard Maestro i: Keyboard Maestro CA>"
)}}, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “10.0.1.19†which could put your confidential information at risk., NSErrorFailingURLKey=https://10.0.1.19:4491/authenticatedaction.html?macro=696B38D0-AF57-4991-83DD-DFD03F1A693B&value=, NSErrorFailingURLStringKey=https://10.0.1.19:4491/authenticatedaction.html?macro=696B38D0-AF57-4991-83DD-DFD03F1A693B&value=, NSErrorClientCertificateStateKey=0}), timeline: Timeline: { "Request Start Time": 517316327.668, "Initial Response Time": 517316327.911, "Request Completed Time": 517316327.911, "Serialization Completed Time": 517316327.912, "Latency": 0.243 secs, "Request Duration": 0.243 secs, "Serialization Duration": 0.001 secs, "Total Duration": 0.244 secs }, _metrics: Optional((Task Interval) <_NSConcreteDateInterval: 0x17002b6e0> (Start Date) 2017-05-24 10:58:47 +0000 + (Duration) 0.251558 seconds = (End Date) 2017-05-24 10:58:47 +0000
(Redirect Count) 0
(Transaction Metrics) (Request) <NSURLRequest: 0x170013cd0> { URL: https://10.0.1.19:4491/authenticatedaction.html?macro=696B38D0-AF57-4991-83DD-DFD03F1A693B&value= }
(Response) (null)
(Fetch Start) 2017-05-24 10:58:47 +0000
(Domain Lookup Start) (null)
(Domain Lookup End) (null)
(Connect Start) (null)
(Secure Connection Start) (null)
(Secure Connection End) (null)
(Connect End) (null)
(Request Start) 2017-05-24 10:58:47 +0000
(Request End) 2017-05-24 10:58:47 +0000
(Response Start) 2017-05-24 10:58:47 +0000
(Response End) (null)
(Protocol Name) (null)
(Proxy Connection) NO
(Reused Connection) YES
(Fetch Type) Unknown
))

【问题讨论】:

    标签: ssl swift3 alamofire


    【解决方案1】:

    我终于解决了我的问题。有两个问题。
    1. 我需要在函数外声明 sessionManager。
    让 manager = SessionManager(configuration: URLSessionConfiguration.default, serverTrustPolicyManager: ServerTrustPolicyManager(policies:["localhost":.disableEvaluation]))
    2. 我在某处读到您需要包含主机的端口号,但这实际上导致了我的问题。
    看起来你只需要写“localhost”而不是“localhost:443”。
    无论如何,这里是禁用所有地址评估的完整代码。
    您无需对 Info.plist 进行任何更改。 Xcode 为您提供的默认 Info.plist 似乎可以正常工作。
    IOS 10.3.2,Xcode 8.3.2,来自 Github 的 Alamofire Master 分支(2017-05-23)

    import UIKit
    import Alamofire
    
    class ViewController: UIViewController {
    
        open class MyServerTrustPolicyManager: ServerTrustPolicyManager {
            open override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
                return ServerTrustPolicy.disableEvaluation
            }
        }
    
        let sessionManager = SessionManager(delegate:SessionDelegate(), serverTrustPolicyManager:MyServerTrustPolicyManager(policies: [:]))
    
        func connect() {
            let user = "user"
            let password = "password"
            let parameters:Parameters = ["parameter1":"value1", "parameter2":"value2"]
            sessionManager.request("https://localhost:443/index.php", parameters:parameters).authenticate(user:user, password:password).responseString {
                response in
                    debugPrint(response.result.value)
                } else {
                    debugPrint(response)
                }
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            connect()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    

    【讨论】:

    • 很好的答案,谢谢:)
    【解决方案2】:

    使用SessionManager时,在github上的Alamofire文档中有说明:

    确保保留对新 SessionManager 实例的引用, 否则,当您的 sessionManager 被释放。

    首先,您可以将其设为全局变量。

    例子:

    let sessionManager: SessionManager = {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "10.0.1.19": .disableEvaluation
        ]
    
        return SessionManager(
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }()
    

    在开发环境中使用自签名或无效证书时,可以添加 NSAppTransportSecurity 并将 Info.plist 中的 NSAllowsArbitraryLoads 设置为 &lt;/true&gt;请注意,不建议将其用于生产环境

    详细解释可见Alamofire documentation

    应用传输安全

    随着 iOS 9 中添加了 App Transport Security (ATS),它是 可能使用自定义的 ServerTrustPolicyManager 和几个 ServerTrustPolicy 对象将不起作用。如果你不断地看到 CFNetwork SSLHandshake failed (-9806) 错误,您可能已经运行 进入这个问题。 Apple 的 ATS 系统克服了整个挑战 系统,除非您在应用程序的 plist 中配置 ATS 设置以 禁用它以允许您的应用评估服务器信任。

    如果你遇到这个问题(自签名的概率很高 证书),您可以通过添加以下内容来解决此问题 到您的 Info.plist。

    <dict>
        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>example.com</key>
                <dict>
                    <key>NSExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <!-- Optional: Specify minimum TLS version -->
                    <key>NSTemporaryExceptionMinimumTLSVersion</key>
                    <string>TLSv1.2</string>
                </dict>
            </dict>
        </dict>
    </dict>
    

    是否需要将 NSExceptionRequiresForwardSecrecy 设置为 NO 取决于您的 TLS 连接是否使用允许的密码 套房。在某些情况下,需要将其设置为 NO。这 NSExceptionAllowsInsecureHTTPLoads 必须设置为 YES 才能 允许 SessionDelegate 接收质询回调。一旦 正在调用挑战回调,ServerTrustPolicyManager 将接管服务器信任评估。您可能还需要 如果您尝试指定 NSTemporaryExceptionMinimumTLSVersion 连接到仅支持低于 1.2 的 TLS 版本的主机。

    建议在生产中始终使用有效证书 环境。

    【讨论】:

    • 啊,谢谢@NajdanTomić。我一开始就声明了 sessionManager,但现在我得到了这个错误。完整的错误在问题中。错误:可选(错误域=NSURLErrorDomain 代码=-1202“此服务器的证书无效。您可能正在连接到伪装成“10.0.1.19”的服务器,这可能会使您的机密信息面临风险。 " UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=你还是要连接到服务器吗? - -
    • 检查您遇到该错误的域是否列在serverTrustPolicies 中并且具有.disableEvaluation 作为值。如果已经设置检查 Info.plist 有:&lt;key&gt;NSAppTransportSecurity&lt;/key&gt; &lt;dict&gt; &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt; &lt;true/&gt; &lt;/dict&gt;
    • 再次感谢。如何检查域在 serverTrustPolicies 中? NSAllowsArbitraryLoads 已经在 Info.plist 中。
    • NSAllowsArbitraryLoads 是否设置为 &lt;/true&gt;?如果不是尝试设置它。当我将其删除或设置为 &lt;/false&gt; 时,我会遇到与您相同的错误。检查this gist 看看它在Info.plist 中的样子。
    猜你喜欢
    • 2015-07-21
    • 2015-03-28
    • 2017-04-07
    • 2019-11-17
    • 2020-02-10
    • 2010-12-15
    • 1970-01-01
    • 2018-08-11
    • 2022-08-04
    相关资源
    最近更新 更多