这种是不验证证书的密钥

AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
policy.validatesDomainName = NO;
manager.securityPolicy = policy;
//manager.securityPolicy = [self customSecurityPolicy];

/**** SSL Pinning ****///验证证书,单项验证。(需要后台给证书,并且改为 cer 格式的,最好找安卓转一下,他们比较方便一点)
- (AFSecurityPolicy*)customSecurityPolicy {
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ios118" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setPinnedCertificates:@[certData]];
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
//[securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];
return securityPolicy;
}

//这个是验证证书,双向验证。

if(challenge.previousFailureCount < 5) {
self.serverTrust = challenge.protectionSpace.serverTrust;
SecTrustResultType result;
SecTrustEvaluate(self.serverTrust, &result);

if(result == kSecTrustResultProceed ||
result == kSecTrustResultUnspecified //The cert is valid, but user has not explicitly accepted/denied. Ok to proceed (Ch 15: iOS PTL :Pg 269)
) {

CFIndex certificateCount = SecTrustGetCertificateCount(self.serverTrust);

NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:(NSUInteger)certificateCount];

for (CFIndex i = 0; i < certificateCount; i++) {

SecCertificateRef certificate = SecTrustGetCertificateAtIndex(self.serverTrust, i);

[trustChain addObject:(__bridge_transfer NSData *)SecCertificateCopyData(certificate)];

}
NSBundle *bundle = [NSBundle mainBundle];
NSArray *paths = [bundle pathsForResourcesOfType:@"der" inDirectory:@"."];
NSMutableArray *certificates = [NSMutableArray arrayWithCapacity:[paths count]];

for (NSString *path in paths) {

NSData *certificateData = [NSData dataWithContentsOfFile:path];

[certificates addObject:certificateData];

}
NSArray *_defaultPinnedCertificates = [[NSArray alloc] initWithArray:certificates];

NSUInteger trustedCertificateCount = 0;

for (NSData *trustChainCertificate in trustChain) {

if ([_defaultPinnedCertificates containsObject:trustChainCertificate]) {

trustedCertificateCount++;

}

}

if (trustedCertificateCount > 0) {

[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

}else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该请求不是可信的" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

[challenge.sender cancelAuthenticationChallenge:challenge];

}

[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

iOS 项目中将 http 改成 https 后需要改动的地方(密钥验证)的更多相关文章

  1. java web项目由http转换成https遇到的各种坑

    java web项目由http转换成https遇到的各种坑 这篇文章写给自己在经历项目由http转换成https遇到的各种坑所做的一份笔记,留给以后自己看,或者和开发的朋友也刚好遇到和我一样的问题的朋 ...

  2. tomcat 容器下web项目由http改为https操作步骤及相关的坑

    一.https介绍:    HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP ...

  3. java只http改成https访问

    目录 生成keystore文件 修改tomcat中的server.xml文件 配置浏览器 生成keystore文件: 1.在tomcat的bin 目录下输入命令:keytool -genkeypair ...

  4. Java项目发现==顺手改成equals之后,会发生什么?

    最近发生一件很尴尬的事情,在维护一个 Java 项目的时候,发现有使用 == 来比较两个对象的属性, 于是顺手就把 == 改成了 equals.悲剧发生...... == 和 equals 的区别 = ...

  5. web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法

    感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...

  6. web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法

    感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...

  7. vue2.0项目中 localhost改成ip地址访问

    这里 你可以写成你的ip  那你的项目只能ip访问了,但是写成0.0.0.0的话 你既可已localhost 访问也可以ip访问 也可以写成 127.0.0.1也可以,也能local访问了和ip访问( ...

  8. 帝国cms修改成https后后台登陆空白的解决办法

    以下方法适用帝国cms7.5版本: 7.5版本已经有了http和https自动识别,但是因为一些疑难杂症的原因,自动识别判断的不准,后台登录也是空白, 我们可以打开e/config.php查找'htt ...

  9. android中将EditText改成不可编辑的状态

    今天在做项目的时候,要想实现一个将EditText变成不可编辑的状态,通过查找博客,发现一个好方法,对于单独的EditText控件我们可以单独设置 1.首先想到在xml中设置Android:edita ...

随机推荐

  1. JSP---JSP中4个容器-pageContext使用

    这里重点只讲pageContext容器的用法哦. 因为另外的3个容器(request,session,application)在前面的servlet中已经演示过很多遍了 容器 作用域 pageCont ...

  2. 现在再开发一个CMS系统还有市场吗?

    现在几个老的开源cms系统phpcms.dedecms  基本都不维护了,大量漏洞没人修补.后台难看无比.有没有必要开发一套新的cms系统.还有这个需求吗?大家说说.

  3. Javascript的简介和使用

    摘要:先讨论JavaScript的由来和一些比较容易混淆的概念,又讨论了JavaScript的实现,紧接着说明了在网页中怎么有效地更好地引入js代码. JavaScript的由来 是从一个简单的输入验 ...

  4. 使用Windows Azure创建Windows系统虚拟机-下

    如何在创建虚拟机之后登录虚拟机 这部分将展示如何登录到虚拟机,所以你可以管理它的设置和你会上面运行的应用程序. 注意: 对于要求和故障排除技巧,请参阅“使用RDP或SSH连接到Azure虚拟机”( C ...

  5. ionic如何uglify和minify你的js,css,image,png....

    Install:   1.ionic start myapp blank      2.cd myapp     3.npm install cordova-uglify or npm install ...

  6. (太强大了) - Linux 性能监控、测试、优化工具

    转: http://www.vpsee.com/2014/09/linux-performance-tools/ Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经 ...

  7. 页面接口防刷 解决思路一nginx

    线上环境 很多接口 如果不做缓存 可能导致有人拿到url  每秒几万次的访问后台程序,导致系统down机. 此处, nginx可以加一层缓存. expires起到控制页面缓存的作用,合理的配置expi ...

  8. 《Effect Java》学习笔记1———创建和销毁对象

    第二章 创建和销毁对象 1.考虑用静态工厂方法代替构造器 四大优势: i. 有名称 ii. 不必在每次调用它们的时候都创建一个新的对象:   iii. 可以返回原返回类型的任何子类型的对象: JDBC ...

  9. bootstrap-table对前台页面表格的支持

    1.bootstrap-table是在bootstrap的基础上面做了一些封装,所以在使用bootstrap-table之前要导入的js和css有 1)基本的还是jQuery <script t ...

  10. children真的没有兼容性问题吗?

    获取某一节点的所有子元素节点,你会用什么方法? 如果你用childNodes,那会有兼容性问题,对于标准浏览器,他包含的是元素节点和文本节点,我们还需要循环来判断节点的类型,是文本节点还是元素节点,不 ...