12345 | < key >NSAPPTransportSecurity</ key > < dict > < key >NSAllowsArbitraryLoads</ key > < true /> </ dict > |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | import UIKit import Alamofire class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //认证相关设置 let manager = SessionManager . default manager.delegate.sessionDidReceiveChallenge = { session, challenge in //认证服务器证书 if challenge.PRotectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { print ( "服务端证书认证!" ) let serverTrust: SecTrust = challenge.protectionSpace.serverTrust! let certificate = SecTrustGetCertificateAtIndex (serverTrust, 0)! let remoteCertificateData = CFBridgingRetain ( SecCertificateCopyData (certificate))! let cerPath = Bundle .main.path(forResource: "tomcat" , ofType: "cer" )! let cerUrl = URL (fileURLWithPath:cerPath) let localCertificateData = try! Data (contentsOf: cerUrl) if (remoteCertificateData.isEqual(localCertificateData) == true ) { let credential = URLCredential (trust: serverTrust) challenge.sender?.use(credential, for : challenge) return ( URLSession . AuthChallengeDisposition .useCredential, URLCredential (trust: challenge.protectionSpace.serverTrust!)) } else { return (.cancelAuthenticationChallenge, nil ) } } //认证客户端证书 else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate { print ( "客户端证书认证!" ) //获取客户端证书相关信息 let identityAndTrust: IdentityAndTrust = self .extractIdentity(); let urlCredential: URLCredential = URLCredential ( identity: identityAndTrust.identityRef, certificates: identityAndTrust.certArray as ? [ AnyObject ], persistence: URLCredential . Persistence .forSession); return (.useCredential, urlCredential); } // 其它情况(不接受认证) else { print ( "其它情况(不接受认证)" ) return (.cancelAuthenticationChallenge, nil ) } } //数据请求 Alamofire .request( "https://192.168.1.112:8443" ) .responseString { response in print (response) } } //获取客户端证书相关信息 func extractIdentity() -> IdentityAndTrust { var identityAndTrust: IdentityAndTrust ! var securityError: OSStatus = errSecSuccess let path: String = Bundle .main.path(forResource: "mykey" , ofType: "p12" )! let PKCS12Data = NSData (contentsOfFile:path)! let key : NSString = kSecImportExportPassphrase as NSString let options : NSDictionary = [key : "123456" ] //客户端证书密码 //create variable for holding security information //var privateKeyRef: SecKeyRef? = nil var items : CFArray ? securityError = SecPKCS12Import ( PKCS12Data , options, &items) 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 > { // grab the identity let identityPointer: AnyObject ? = certEntry[ "identity" ]; let secIdentityRef: SecIdentity = identityPointer as ! SecIdentity ! print ( "/(identityPointer) :::: /(secIdentityRef)" ) // grab the trust let trustPointer: AnyObject ? = certEntry[ "trust" ] let trustRef: SecTrust = trustPointer as ! SecTrust print ( "/(trustPointer) :::: /(trustRef)" ) // grab the cert let chainPointer: AnyObject ? = certEntry[ "chain" ] identityAndTrust = IdentityAndTrust (identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!) } } return identityAndTrust; } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } //定义一个结构体,存储认证相关信息 struct IdentityAndTrust { var identityRef: SecIdentity var trust: SecTrust var certArray: AnyObject } |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | import UIKit import Alamofire class ViewController : UIViewController { //自签名网站地址 let selfSignedHosts = [ "192.168.1.112" , "www.hangge.com" ] override func viewDidLoad() { super .viewDidLoad() //认证相关设置 let manager = SessionManager . default manager.delegate.sessionDidReceiveChallenge = { session, challenge in //认证服务器(这里不使用服务器证书认证,只需地址是我们定义的几个地址即可信任) if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && self .selfSignedHosts.contains(challenge.protectionSpace.host) { print ( "服务器认证!" ) let credential = URLCredential (trust: challenge.protectionSpace.serverTrust!) return (.useCredential, credential) } //认证客户端证书 else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate { print ( "客户端证书认证!" ) //获取客户端证书相关信息 let identityAndTrust: IdentityAndTrust = self .extractIdentity(); let urlCredential: URLCredential = URLCredential ( identity: identityAndTrust.identityRef, certificates: identityAndTrust.certArray as ? [ AnyObject ], persistence: URLCredential . Persistence .forSession); return (.useCredential, urlCredential); } // 其它情况(不接受认证) else { print ( "其它情况(不接受认证)" ) return (.cancelAuthenticationChallenge, nil ) } } //数据请求 Alamofire .request( "https://192.168.1.112:8443" ) .responseString { response in print (response) } } //获取客户端证书相关信息 func extractIdentity() -> IdentityAndTrust { var identityAndTrust: IdentityAndTrust ! var securityError: OSStatus = errSecSuccess let path: String = Bundle .main.path(forResource: "mykey" , ofType: "p12" )! let PKCS12Data = NSData (contentsOfFile:path)! let key : NSString = kSecImportExportPassphrase as NSString let options : NSDictionary = [key : "123456" ] //客户端证书密码 //create variable for holding security information //var privateKeyRef: SecKeyRef? = nil var items : CFArray ? securityError = SecPKCS12Import ( PKCS12Data , options, &items) 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 > { // grab the identity let identityPointer: AnyObject ? = certEntry[ "identity" ]; let secIdentityRef: SecIdentity = identityPointer as ! SecIdentity ! print ( "/(identityPointer) :::: /(secIdentityRef)" ) // grab the trust let trustPointer: AnyObject ? = certEntry[ "trust" ] let trustRef: SecTrust = trustPointer as ! SecTrust print ( "/(trustPointer) :::: /(trustRef)" ) // grab the cert let chainPointer: AnyObject ? = certEntry[ "chain" ] identityAndTrust = IdentityAndTrust (identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!) } } return identityAndTrust; } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } //定义一个结构体,存储认证相关信息 struct IdentityAndTrust { var identityRef: SecIdentity var trust: SecTrust var certArray: AnyObject } |
新闻热点
疑难解答