Alamofire公钥固定无法验证其他网址

时间:2019-04-11 08:28:08

标签: swift ssl-certificate alamofire certificate-pinning

我有3个baseURL,这3个拥有不同的ssl证书,即使我更改了url请求,应用也未阻止该请求,我已经下载了一个证书并在Xcode项目中进行了硬编码。

我正在使用此类

class APIManager {

    static let sharedInstance = APIManager()
    private var manager : SessionManager?
    func sessionManager() -> SessionManager {

        if let manage = self.manager {
            return manage
        }
        else {
            let serverTrustpolicies: [ String: ServerTrustPolicy] = ["url" : .pinPublicKeys(publicKeys: ServerTrustPolicy.publicKeys(), validateCertificateChain: true, validateHost: true)
            ]

            self.manager = SessionManager(
                serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustpolicies)
            )
            let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = 30
            self.manager = SessionManager(configuration: configuration)
            return self.manager!
        }

    }
}

APIManager.sharedInstance.sessionManager().upload(multipartFormData: { (multipartFormData)
}

有人可以解释实施吗?

1 个答案:

答案 0 :(得分:0)

您必须为要验证的每个主机显式设置ServerTrustPolicy值。因此,在创建策略字典时,您需要传递每个主机。

let serverTrustPolicies: [String: ServerTrustPolicy] = ["url" : .pinPublicKeys(publicKeys: ServerTrustPolicy.publicKeys(), validateCertificateChain: true, validateHost: true)]

这应该添加您的其他基本URL,并设置适当的策略。