在Alamofire上使用DisableEvaluation信任无效证书

时间:2018-11-16 10:22:53

标签: ios swift alamofire

我需要访问具有无效证书和基本身份验证的API。在搜索时,我需要编写自定义SessionManager并将一个值添加到plist文件中。经过几天的搜索和大量发布,我仍然无法访问API。

struct CustomManagerClass{

static let instance = CustomManagerClass()
var sessionManager : SessionManager = {

    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "baseurl.com:8443": .disableEvaluation
    ]

    // Create custom manager
    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
    let manager = Alamofire.SessionManager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

    return manager
}()

列表文件:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>baseurl.com</key>
    <dict>
        <key>NSTemporaryExceptionMinimumTLSVersion</key>
        <string>TLSv1.2</string>
        <key>NSIncludesSubdomains</key>
        <true/>
        <key>NSExceptionRequiresForwardSecrecy</key>
        <false/>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
    </dict>
</dict>

我怎么称呼:

CustomManagerClass.instance.sessionManager.request(route).responseJSON(completionHandler: { (result) in
        completion(result)
    }) //Route in here is a ServiceConfiguration class which defines http method, parameters and basic auth.

它仍然返回;

  

任务<4CE5991B-2650-471C-AB77-69D54B8E36F3>。<1>已完成,错误-代码:-1202   该服务器的证书无效。您可能正在连接到假装为“ baseurl.com”的服务器,这可能会使您的机密信息受到威胁。

我得到帮助的帖子: Certificate Invalid Issue with Alamofire 4.0

How to use Alamofires ServerTrustPolicy.disableEvaluation in swift 3

编辑:我将以下代码添加到信任证书。现在它返回HTTP 500

 CustomManagerClass.instance.sessionManager.delegate.sessionDidReceiveChallenge = { session, challenge in
        var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
        var credential: URLCredential?

        print("received challenge")

        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
            disposition = URLSession.AuthChallengeDisposition.useCredential
            credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        } else {
            if challenge.previousFailureCount > 0 {
                disposition = .cancelAuthenticationChallenge
            } else {
                credential = CustomManagerClass.instance.sessionManager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)

                if credential != nil {
                    disposition = .useCredential
                }
            }
        }

        return (disposition, credential)
    }

1 个答案:

答案 0 :(得分:1)

我建议为SessionManager委托上的 sessionDidReceiveChallenge 事件添加此处理程序。

let challengeHandler: ((URLSession, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))? = { result, challenge in
    return (.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}

然后以这种方式初始化manager对象时分配闭包。

manager.delegate.sessionDidReceiveChallenge = challengeHandler

此外,由于处理程序将忽略它将收到的所有信任挑战,因此serverTrustPolicies可以为空。