这种是不验证证书的密钥

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. 关于 Lua 内存泄漏的检测

    前一阵开始和同事一起优化内存,首先是优化 Lua 内存,因为发现每次战斗完后 Lua 内存非常大,从 3M 左右在经过了10次左右的战斗后,会暴增到近 100M,很明显是有内存泄漏.      然后我 ...

  2. 关于Lua 5.1中的debug.hook和coroutine

    1.debug.hook只hook主线程,在coroutine中不起作用. 2.coroutine.resume之后主线程会挂起,直到协程coroutine.yield返回 3. 对于如下代码 deb ...

  3. insertion Sort List (链表的插入排序) leecode java

    逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的 提交地址https://oj.leetcode.com/problems ...

  4. Html笔记(三)列表

    列表标签: <dl> <dt>:上层标签 <dd>:下层标签 例: <dl> <dt>上层项目</dt> <dd>下 ...

  5. 各种sensor名称统计

    gyroscopes 陀螺仪accelerometers 加速度计magnetometers 磁力计barometric pressure 气压remote pressure sensing 远程压力 ...

  6. 关于int **

    例子1: int foo(int *array) { array = (); ; } //外头调用 int * uidArray = NULL; foo(uidArray): 这时候我想要在函数里给数 ...

  7. HDOJ-ACM1071(JAVA) 定积分

    这道题做起来有点无奈,定积分已经忘得差不多了~还可恶的去搜索了抛物线的解析式的求法~哈哈 不过求出来的结果不对...等有时间再去研究这个数学问题吧 - - 以下是JAVA实现: import java ...

  8. ZOJ3471--Most Powerful(状压DP)

    有n(n<10)个小球,每两个碰撞有其中一个会消失,并产生一定能量.求产生最大能量. 有了正确思路题目比较简单,我的思路果然是歪的... 我的思路是dp[i][j]表示i这个状态剩下第j个小球, ...

  9. POJ2923--Relocation(01背包+状压dp)

    果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Mem ...

  10. 最新版postgresql+pgboucer安装

    安装postgresql: wget https://pgbouncer.github.io/downloads/files/1.7.2/pgbouncer-1.7.2.tar.gz wget htt ...