ios 配置HTTPS (单向验证,工程不需要加证书)
2017-02-23 22:02
639 查看
这个问题坑了我两天啊!网上各种搜,也没解决了。
后台单向验证,自签名证书!已更新 : oc AFNetworking 适配 HTTPS
客户端需要做的事情:
一、swift Alamofire网络请求
在请求方法前面加上以下代码即可:let manager: Alamofire.Manager = { let manager = Alamofire.Manager.sharedInstance manager.delegate.sessionDidReceiveChallenge = { session, challenge in var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling var credential: NSURLCredential? if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { disposition = NSURLSessionAuthChallengeDisposition.UseCredential credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!) } else { if challenge.previousFailureCount > 0 { disposition = .CancelAuthenticationChallenge } else { credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace) if credential != nil { disposition = .UseCredential } } } return (disposition, credential) } return manager }()
二、 UIWebView 配置https(因为我的工程全是swift写的,贴上swift代码,oc自己转吧!)
主要实现NSURLConnectionDelegate两个代理方法:func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool { return (protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) } func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) { challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge) challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge) } }这样就搞定了 ,坑了我两天啊,swift的更是不好找!
三、 OC AFNetworking 适配HTTPS 工程不需要添加证书 单向验证 请求处加上一下代码 就OK
//适配HTTPS AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; [securityPolicy setValidatesDomainName:NO]; securityPolicy.allowInvalidCertificates = YES; //还是必须设成YES manager.securityPolicy = securityPolicy;
相关文章推荐
- iOS 迁移到HTTPS(自建证书+单向验证+AF3.0+WKWebView+SDWebImage)
- iOS 迁移到HTTPS(自建证书+单向验证+AF3.0+WKWebView+SDWebImage)
- 【网页访问单向、双向验证均可以】https原理及tomcat配置https方法[生成CA根证书配置tomcat后,若要成功访问axis中的webservice,需要配置它对应的axis2.xml文件]
- Tomcat配置https单向双向认证,iOS加密解密验证,iOS访问HTTPS
- Tomcat配置https单向双向认证,iOS加密解密验证,iOS访问HTTPS
- https 单向认证和双向认证及tomcat配置及各种证书类型之间的转换,适用ios,Android,网页端
- 【腾讯Bugly干货分享】iOS 中 HTTPS 证书验证浅析
- 轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https
- iOS开发-https免证书验证
- 详解iOS开发 - 用AFNetworking实现https单向验证,双向验证
- TOMCAT配置HTTPS生成单向证书问题
- ios https单向验证
- iOS HTTPS安全请求 验证服务器返回的证书
- HttpClient之配置ssl,采用绕过证书验证实现https
- 【腾讯Bugly干货分享】iOS 中 HTTPS 证书验证浅析
- iOS UIWebView 访问https 绕过证书验证的方法
- 轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https
- iOS UIWebView 访问https绕过证书验证的方法
- iOS UIWebView 访问https 绕过证书验证的方法
- ssl https双向验证的配置与证书库的生成