Kingfisher verifying a self-signed certificate download HTTPS pictures
Let Downer =
KingfisherManager.shared.downloader
downer.trustedhosts = Set (["Host Address"])
It's so simple.
Alamofire:
The Servertrustpolicy enumeration uses Pincertificates. Case Pincertificates (certificates: [Seccertificate], Validatecertificatechain:bool, Validatehost:bool) incoming self-signed certificate information. Example code: Let Path = bundle.main. Path (forresource: "Selfsigned_pubca.cer", oftype:nil) Let data = NSData (Contentsoffile:pat h!) Let certificates: [seccertificate] = [Data as! Seccertificate] Let policies: [String:servertrustpolicy] = ["172.16.88.230":. Pincertificates (Certificates:certifica TES, Validatecertificatechain:true, Validatehost:true)] "Pass this policies as a parameter into the manager constructor, butLet certificates: [Seccertificate] = [Data as! Seccertificate] But there's a collapse here, not a strong turn. Next scenario:
Define a struct to store authentication-related information
struct Identityandtrust {
var identityref:secidentity
var trust:sectrust
var certarray:anyobject
}
Func customextractidentity (), Identityandtrust {
var identityandtrust:identityandtrust!
var securityerror:osstatus = errsecsuccess
Let cerpath:string? = Bundle.main.path (forresource: "EIDSP", OfType: "P12")!
Print ("cerpath%@", Cerpath as Any)
Let Cerdata = NSData (contentsoffile:cerpath!)!
Print ("cerdata%@", cerdata as Any)
Let key:nsstring = Ksecimportexportpassphrase as NSString
Let options:nsdictionary = [key: ""]//Client certificate Password
var items:cfarray?
Securityerror = Secpkcs12import (cerdata, Options, &items)
Print ("securityerror%@", Securityerror)
if Securityerror = = errsecsuccess {
Let Certitems:cfarray = items as cfarray!;
Let Certitemsarray:array = Certitems as Array
Let Dict:anyobject? = Certitemsarray.first;
If let certentry:dictionary = Dict as? Dictionary<string, anyobject> {
Let Identitypointer:anyobject? = certentry["Identity"];
Let secidentityref:secidentity = Identitypointer as! secidentity!
Let Trustpointer:anyobject? = certentry["Trust"]
Let Trustref:sectrust = Trustpointer as! Sectrust
Let Chainpointer:anyobject? = certentry["Chain"]
Identityandtrust = Identityandtrust (Identityref:secidentityref,
Trust:trustref, certarray:chainpointer!)
}
}
return identityandtrust;
}
Func Httpsmanager () {
Manager?. Delegate.sessiondidreceivechallenge = {session, challenge in
Print ("Challenge.protectionSpace.authenticationMethod!!!!!!!!!!! %@ ", Challenge.protectionSpace.authenticationMethod)
If Challenge.protectionSpace.authenticationMethod
= = Nsurlauthenticationmethodservertrust
{print ("Server Authentication! ")
Let credential = urlcredential (trust:challenge.protectionspace.servertrust!)
Return (. Usecredential, credential)
}
else if Challenge.protectionSpace.authenticationMethod
= = Nsurlauthenticationmethodclientcertificate {
Print ("Client certificate authentication!") ")
Let Identityandtrust:identityandtrust = customextractidentity ();
Let urlcredential:urlcredential = Urlcredential (
Identity:identityAndTrust.identityRef,
Certificates:identityAndTrust.certArray as? [Anyobject],
Persistence:URLCredential.Persistence.forSession);
Return (. usecredential, urlcredential);
}
else {
Print ("Other cases (not accepted)")
Return (. Cancelauthenticationchallenge, nil)
}
}
}
Put Httpsmanager () This method in the tool class every request is called once, I know that I am a stupid method if there is a way to do well, please inform!
Alamofire + Kingfisher Https self-signed certificate