发生SSL错误,无法与服务器建立安全连接

时间:2016-06-07 10:01:28

标签: ios security ssl alamofire nsurlsession

正如标题所说。我尝试过使用NSURLSession和Alamofire。

两者都给了我同样的错误。我正在使用模拟器,无法访问设备。我有限制,需要它在模拟器上工作。

我正在尝试从服务器检索用户登录详细信息(同样,我无法访问的服务器,因为另一个团队负责。

let headers = [
    "content-type": "application/json",
    "accept": "application/json",
    "appid": "test",
    "cache-control": "no-cache",
    "postman-token": "xxxx-xxxx-xxxx-xxxx-xxxx"
]
let parameters = ["attributes": [
    "password": "test",
    "devicename": "My smartphone",
    "deviceid": "ABC99999999",
    "email": "xxxx@xxxx.co.uk"
    ]]


do {

    let postData = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)

    let request = NSMutableURLRequest(URL: NSURL(string: "https://a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net:443/Identity/rest/appl/test/wflow/auth")!,
                                      cachePolicy: .UseProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    request.HTTPMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.HTTPBody = postData

    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error?.localizedDescription)
        } else {
            let httpResponse = response as? NSHTTPURLResponse
            print(httpResponse)
        }
    })

    dataTask.resume()

} catch {

    print(error)
}

网址仅供内部使用,因此无法在我们的网络之外使用。

这些是我经常遇到的错误。

"发生了SSL错误,无法与服务器建立安全连接。"

"此服务器的证书无效。您可能正在连接到假装为“a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net”的服务器,这可能会使您的机密信息面临风险。"

我在线搜索和搜索但找不到任何解决方案。

这就是我的info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net</key>
        <dict>
            <key>NSRequiresCertificateTransparency</key>
            <string>NO</string>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <string>YES</string>
            <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
            <false/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludeSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

我已查看以下帖子并已将其应用但未取得任何成功:

How to connect to self signed servers using Alamofire 1.3

iOS 9.3 : An SSL error has occurred and a secure connection to the server cannot be made

iOS9 getting error “an ssl error has occurred and a secure connection to the server cannot be made”

更新1: 我忘了添加,当我使用Alamofire时,我也试图忽略服务器策略或通过下面的方法绕过它们,再次没有一个工作:(

let manager : Alamofire.Manager = {
    // Create the server trust policies
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net": .DisableEvaluation
    ]
    // Create custom manager
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
    let man = Alamofire.Manager(
        configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )
    return man
}()

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)
}

0 个答案:

没有答案