iOS 项目中将 http 改成 https 后需要改动的地方(密钥验证)
这种是不验证证书的密钥
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 后需要改动的地方(密钥验证)的更多相关文章
- java web项目由http转换成https遇到的各种坑
java web项目由http转换成https遇到的各种坑 这篇文章写给自己在经历项目由http转换成https遇到的各种坑所做的一份笔记,留给以后自己看,或者和开发的朋友也刚好遇到和我一样的问题的朋 ...
- tomcat 容器下web项目由http改为https操作步骤及相关的坑
一.https介绍: HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP ...
- java只http改成https访问
目录 生成keystore文件 修改tomcat中的server.xml文件 配置浏览器 生成keystore文件: 1.在tomcat的bin 目录下输入命令:keytool -genkeypair ...
- Java项目发现==顺手改成equals之后,会发生什么?
最近发生一件很尴尬的事情,在维护一个 Java 项目的时候,发现有使用 == 来比较两个对象的属性, 于是顺手就把 == 改成了 equals.悲剧发生...... == 和 equals 的区别 = ...
- web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法
感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...
- web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法
感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...
- vue2.0项目中 localhost改成ip地址访问
这里 你可以写成你的ip 那你的项目只能ip访问了,但是写成0.0.0.0的话 你既可已localhost 访问也可以ip访问 也可以写成 127.0.0.1也可以,也能local访问了和ip访问( ...
- 帝国cms修改成https后后台登陆空白的解决办法
以下方法适用帝国cms7.5版本: 7.5版本已经有了http和https自动识别,但是因为一些疑难杂症的原因,自动识别判断的不准,后台登录也是空白, 我们可以打开e/config.php查找'htt ...
- android中将EditText改成不可编辑的状态
今天在做项目的时候,要想实现一个将EditText变成不可编辑的状态,通过查找博客,发现一个好方法,对于单独的EditText控件我们可以单独设置 1.首先想到在xml中设置Android:edita ...
随机推荐
- 谈谈以下关键字的作用auto static register const volatile extern
(1)auto 这个这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是 ...
- Html笔记(九)头标签
头标签: <head> 头标签都放在 <head> </head> 头部分之间.包括:title base meta link <title> :指定浏 ...
- Codeforces335B - Palindrome(区间DP)
题目大意 给定一个长度不超过5*10^4的只包含小写字母的字符串,要求你求它的回文子序列,如果存在长度为100的回文子序列,那么只要输出长度为一百的回文子序列即可,否则输出它的最长回文子序列 题解 这 ...
- 中局域网LAN中建立局域网可访问的类GitHub的服务器
There are several ways to do this Host internal repositories like Gitlab (free software) or Stash. T ...
- 查看当前正在被执行的sql
由于在公司服务器上在某个时段查询某个sql执行比较慢,由来查询当前正在被执行的sql Select t.text,SUBSTRING(t.text, (r.statement_start_offset ...
- 在Android中访问内置SE和基于SE的卡模拟(一)
2013-10-10 编写 前言 在“十问Android NFC手机上的卡模拟”文中仅仅简单的介绍了一下相关的概念,如果需要了解基于SE的卡模拟的更多细节,也就是,究竟在Android的NFC手机上, ...
- win 8(win 7)批处理设置IP
适合所有经常更改IP的朋友,里面的内容可用可用根据自己的需要随意修改 @rem 根据自己的需要修改带 (@rem/注释)的地方,修改完毕后直接将本文件后缀名.txt改为.bat即可使用 @rem 运行 ...
- xcode针对不同IOS版本的代码编译问题
有时候在项目中为了兼容低版本IOS系统,通常会针对不同的OS版本写不同的代码,例如: #define IS_IOS7_OR_LATER ([[UIDevice currentDevice].syste ...
- ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限
开发了一个导入TXT文件的功能,执行过程中出错.提示:.....ASP.NET 未被授权访问所请求的资源.请考虑授予 ASP.NET 请求标识访问此资源的权限.ASP.NET 有一个在应用程序没有模拟 ...
- Linux下history命令详解---转载
Linux下History命令主要用于显示历史指令记录内容, 下达历史纪录中的指令 . >History命令语法:[www.linuxidc.com@linux]# history [n][ww ...