iOS Alamofire SSL固定

时间:2019-12-10 10:19:04

标签: ios swift alamofire

您好,我对ssl固定证书有疑问,这是我的代码:

var afManager:SessionManager?

私有功能enableCertificatePinning(){

   let certificates = getCertificates()
      let trustPolicy = ServerTrustPolicy.pinCertificates(
          certificates: certificates,
          validateCertificateChain: true,
          validateHost: true)
      let trustPolicies = [ "domain": trustPolicy ]
      let policyManager = ServerTrustPolicyManager(policies: trustPolicies)
      afManager = SessionManager(
          configuration: .default,
          serverTrustPolicyManager: policyManager
      )

    }




    private func getCertificates() -> [SecCertificate] {
        let url = Bundle.main.url(forResource: "my certificate name", withExtension: "cer")!
        let localCertificate = try! Data(contentsOf: url) as CFData
        guard let certificate = SecCertificateCreateWithData(nil, localCertificate)
           else { return [] }

        return [certificate]
    }

此代码不阻止请求

0 个答案:

没有答案