美文网首页
Alamofire https证书验证

Alamofire https证书验证

作者: 十一岁的加重 | 来源:发表于2019-03-04 13:08 被阅读0次

    1、公钥验证

    let pathToCert1 = Bundle.main.path(forResource: "cer1", ofType: "cer")
            let pathToCert2 = Bundle.main.path(forResource: "cer2", ofType: "cer")
    
            let localCertificate = NSData(contentsOfFile: pathToCert1!)
            let localCertificate2 = NSData(contentsOfFile: pathToCert2!)
    
            let certificates = [SecCertificateCreateWithData(nil, localCertificate1!)!, SecCertificateCreateWithData(nil, localCertificate2!)!]
            
            let serverTrustPolicy = ServerTrustPolicy.pinPublicKeys(
                publicKeys: certificates as! [SecKey],
                validateCertificateChain: true,
                validateHost: true
            )
            
            
            let serverTrustPolicies = ["www.text.com" : serverTrustPolicy]
            let serverTrustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies)
            return Alamofire.SessionManager(
                configuration: URLSessionConfiguration.default,
                serverTrustPolicyManager: serverTrustPolicyManager
            )
    

    2、全验证

    let trustPolicyManager = ServerTrustPolicyManager(policies: [
               "www.text.com": .pinCertificates(
                  certificates:ServerTrustPolicy.certificates(in: Bundle.main),
                    validateCertificateChain: true,
                    validateHost: true),
                ]);
            let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
           return Alamofire.SessionManager(configuration: configuration,
                                           serverTrustPolicyManager: trustPolicyManager);
    

    相关文章

      网友评论

          本文标题:Alamofire https证书验证

          本文链接:https://www.haomeiwen.com/subject/opobuqtx.html