当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS开发 支持https请求以及ssl证书配置详解

IOS开发 支持https请求以及ssl证书配置详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios开发 支持https请求以及ssl证书配置详解

前言:

众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https

楼主正好近日将http转为https,给还没动手的朋友分享一二

一、证书准备

1、证书转换

在服务器人员,给你发送的crt证书后,进到证书路径,执行下面语句

// openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der

这样你就可以得到cer类型的证书了。双击,导入电脑。

2、证书放入工程

1、可以直接把转换好的cer文件拖动到工程中。

2、可以在钥匙串内,找到你导入的证书,单击右键,导出项目,就可以导出.cer文件的证书了

二、代码准备

<key>nsapptransportsecurity</key>
  <dict>
    <key>nsallowsarbitraryloads</key>
    <true/>
  </dict>

1.1 nsurlconnection设置支持https。

在2015年ios9的更新中,nsurlconnection 被废弃 由 nsurlsession 取代,所以本身是不建议大家继续用这个类做网络请求的(同样也有afnetworking 2.x版本),但是考虑到一些旧程序,也不能说改就改,说替换就替换的,所以还是需要普及一下,如果用到了nsurlconnection你需要怎么做。

代码如下:

- (void)connection:(nsurlconnection *)connection willsendrequestforauthenticationchallenge:(nsurlauthenticationchallenge *)challenge{

  if(challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust) {
    // 告诉服务器,客户端信任证书
    // 创建凭据对象
    nsurlcredential *credntial = [nsurlcredential credentialfortrust:challenge.protectionspace.servertrust];
    // 告诉服务器信任证书
    [challenge.sender usecredential:credntial forauthenticationchallenge:challenge];
  }
}

你只需要简单的,添加上如上的代理方法,就可以在不影响你原有请求的基础上,增加了https请求的支持了。

1.2 nsurlsession设置支持https。

现在推荐使用的就是nsurlsession来处理相关的网络请求了,如果使用系统自带的类,可以参考如下代码:

- (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task didreceivechallenge:(nsurlauthenticationchallenge *)challenge completionhandler:(void (^)(nsurlsessionauthchallengedisposition disposition, nsurlcredential * __nullable credential))completionhandler {

  // 判断是否是信任服务器证书
  if(challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust) {
    // 告诉服务器,客户端信任证书
    // 创建凭据对象
    nsurlcredential *credntial = [nsurlcredential credentialfortrust:challenge.protectionspace.servertrust];
    // 通过completionhandler告诉服务器信任证书
    completionhandler(nsurlsessionauthchallengeusecredential,credntial);
  }
  nslog(@"protectionspace = %@",challenge.protectionspace);
}

2.使用afnetworking发送网络请求篇

afnetworking是一个讨人喜欢的网络库,适用于ios以及mac os x. 它构建于在nsurlconnection, nsoperation, 以及其他熟悉的foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.。

2.1 afnetworking 2.x版本

考虑到这个版本,我们还可以使用afhttprequestoperationmanager这个类来处理网络请求。所以我们要做的就是给这个类,设置一些参数,让它可以支持https的请求,代码如下:

支持https(校验证书,不可以抓包):

// 1.初始化单例类
  afhttprequestoperationmanager *mgr = [afhttprequestoperationmanager manager];
  mgr.securitypolicy.sslpinningmode = afsslpinningmodecertificate;
  // 2.设置证书模式
  nsstring * cerpath = [[nsbundle mainbundle] pathforresource:@"xxx" oftype:@"cer"];
  nsdata * cerdata = [nsdata datawithcontentsoffile:cerpath];
  mgr.securitypolicy.pinnedcertificates = [[nsarray alloc] initwithobjects:cerdata, nil];
  // 客户端是否信任非法证书
  mgr.securitypolicy.allowinvalidcertificates = yes;
  // 是否在证书域字段中验证域名
  [mgr.securitypolicy setvalidatesdomainname:no];

支持https(不校验证书,可以抓包查看):

 // 1.初始化单例类
  afhttprequestoperationmanager *mgr = [afhttprequestoperationmanager manager];
  mgr.securitypolicy.sslpinningmode = afsslpinningmodecertificate;
  // 2.设置非校验证书模式
  mgr.securitypolicy = [afsecuritypolicy policywithpinningmode:afsslpinningmodenone];
  mgr.securitypolicy.allowinvalidcertificates = yes;
  [mgr.securitypolicy setvalidatesdomainname:no];

2.2 afnetworking 3.x版本

在xcode7.0之后,苹果废弃了nsurlconnection方法,数据请求使用nsurlsession,作为网络请求类第三方库使用量最大的afn也及时的更新的新的版本——afn 3.0版本。新的版本的里废弃了基于nsurlconnection封装的afhttprequestoperationmanager,转而使用基于nsurlsession封装的afhttpsessionmanager了。

支持https(校验证书,不可以抓包):

// 1.初始化
   afhttpsessionmanager *manager = [afhttpsessionmanager manager];
  manager.securitypolicy.sslpinningmode = afsslpinningmodecertificate;
  // 2.设置证书模式
  nsstring * cerpath = [[nsbundle mainbundle] pathforresource:@"xxx" oftype:@"cer"];
  nsdata * cerdata = [nsdata datawithcontentsoffile:cerpath];
  manager.securitypolicy = [afsecuritypolicy policywithpinningmode:afsslpinningmodecertificate withpinnedcertificates:[[nsset alloc] initwithobjects:cerdata, nil]];
  // 客户端是否信任非法证书
  mgr.securitypolicy.allowinvalidcertificates = yes;
  // 是否在证书域字段中验证域名
  [mgr.securitypolicy setvalidatesdomainname:no];

支持https(不校验证书,可以抓包查看):

// 1.初始化
   afhttpsessionmanager *manager = [afhttpsessionmanager manager];
  // 2.设置非校验证书模式
  manager.securitypolicy = [afsecuritypolicy policywithpinningmode:afsslpinningmodenone];
  manager.securitypolicy.allowinvalidcertificates = yes;
  [manager.securitypolicy setvalidatesdomainname:no];

到这里配置就完成了,希望对你有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网