Alamofire https证书验证
2019-03-04 本文已影响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);