尝试使用swift从自签名服务器获取数据,用于iOS应用程序开发

时间:2017-08-03 11:06:19

标签: ios swift ssl alamofire

首先我尝试了info.plist方式,使用

使我的网站成为例外
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>service.medimetry.dev</key>
        <dict>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>

仍显示错误&#34;发生SSL错误,无法与服务器建立安全连接。&#34; 尝试了5种不同的上层代码后,我开始寻找另一个角度。 我尝试使用swiftHTTP方法绕过使用此代码的SSL身份验证

func getAllReminders () {
    do {
        let opt = try HTTP.GET("https://service.medimetry.dev/api/v2/reminders")
        var attempted = false
        opt.auth = { challenge in
            if !attempted {
                attempted = true
                return URLCredential(forTrust: challenge.protectionSpace.serverTrust)
            }
            return nil
        }

        opt.start { response in
            if let err = response.error {
                print("error: \(err.localizedDescription)")
                return //also notify app of failure as needed
            }
            print("opt finished: \(response.description)")

            print("Response Loggin starts >>>")
            print(response.data)
            print("<<< Response Loggin ends")
        }
    } catch let error {
        print("got an error creating the request: \(error)")
    }
}

我收到了这个错误&#39;&#34; challenge.protectionSpace.serverTrust&#34;不匹配可用的重载&#39;试图找到解决方法,但是不能。

然后谷歌所有包的http和发现Alamofire 也有同样的问题,尝试谷歌搜索,发现这写了这段代码

func getAllReminders () {
    defaultManager.request("https://service.medimetry.dev/api/v2/reminders").responseJSON { response in
        print("Request: \(String(describing: response.request))")   // original url request
        print("Response: \(String(describing: response.response))") // http url response
        print("Result: \(response.result)")                         // response serialization result

        if let json = response.result.value {
            print("JSON: \(json)") // serialized json response
        }

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)") // original server data as UTF8 string
        }
    }
}

let defaultManager: Alamofire.SessionManager = {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "127.0.0.1:443": .disableEvaluation,
        "service.medimetry.dev:443": .disableEvaluation,
        "https://service.medimetry.dev:443": .disableEvaluation,
        "127.0.0.1:80": .disableEvaluation,
        "service.medimetry.dev:80": .disableEvaluation,
        "https://service.medimetry.dev:80": .disableEvaluation,
        ]

    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders

    return Alamofire.SessionManager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )
}()

但又是这个回应 2017-08-03 16:29:34.369747 + 0530提醒系统[5364:433698] [] nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue:[ - 9812] 2017-08-03 16:29:34.369提醒系统[5364:433712] NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813) 请求:可选(https://service.medimetry.dev/api/v2/reminders) 回复:没有 结果:失败 数据:

我在这个问题上花了好几个小时,请告诉我我做错了什么。在上面提到的代码中的任何修复,或任何其他方式,在生产中我将拥有所有正确的服务器,但只需要在调试模式下的方法。 在alamofire中,我尝试过this。如果有简单的方法使用justHTTP pod。 对于服务器,我使用带有安全标志的laravel代客。

1 个答案:

答案 0 :(得分:0)

只需在plist中添加这些键

<key>NSIncludesSubdomains</key> <true/> 
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> 
<key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> 

它应该适用于自签名证书