Alamofire 的 SSL 证书固定在 iOS 中不起作用

问题描述 投票:0回答:1

我尝试在 swift 项目中使用 alamofire 实现带有证书的 ssl pinning,但出现 noCertificateFound 错误。

let evaluator : [String : ServerTrustEvaluating] = ["staging-clientsapi.expatpa.com": PinnedCertificatesTrustEvaluator(certificates: AFWrapper.pinnedCertificates())]
let manager = ServerTrustManager(evaluators: evaluator)
afSession = Session.init(serverTrustManager: manager)
    static func pinnedCertificates() -> [SecCertificate] {
        var certificates: [SecCertificate] = []
        if let pinnedCertificateURL = Bundle.main.url(forResource: "expatpa.com", withExtension: "cer") {
            do {
                let pinnedCertificateData: CFData = try Data(contentsOf: pinnedCertificateURL) as CFData
                if let pinnedCertificate: SecCertificate = SecCertificateCreateWithData(nil, pinnedCertificateData) {
                    certificates.append(pinnedCertificate)
                }
            } catch {
                print("certicate exception--")
            }
        }
        return certificates
    }

    afSession.request(request).validate().responseString { .... }
ios swift ssl alamofire
1个回答
0
投票

.noCertificatesFound
表示您的
Bundle.main.url(forResource: "expatpa.com", withExtension: "cer")
没有返回任何内容,并将一个空数组传递给评估器。我建议您只使用 Alamofire 的默认
.pinnedCertificates
评估器,默认情况下它将找到主包中的所有证书。

© www.soinside.com 2019 - 2024. All rights reserved.