如何理解AFNetWorking https双向认证

技术如何理解AFNetWorking https双向认证这篇文章给大家介绍如何理解AFNetWorking https双向认证,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。客户端验证服务端证书:需要

这篇文章给大家介绍如何理解AFNetWorking https双向认证,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

客户端验证服务端证书:

需要超文本传送协议(超文本传输协议的缩写)配置路径需要域名

1:先项目中倒入服务端证书sever.cer,

2.然后设置安全政策

自我。manager=[AFHTTPRequestOperationManager manager];

自我。经理。response serializer=[[afhttpresponse serializer alloc]init];

[自我。经理。http ader field : @“header-platform ”]的请求序列化程序set value : @“iphone ”;

自我。经理。安全策略=[具有锁定模式: afsslpinningmodepublikey的afsecurity policy策略];

自我。经理。安全政策。允许有效证书=是;

自我。经理。安全政策。validateddomain域名=否;

自我。经理。安全政策。验证证书链=否;

客户端会变了项目中的证书和服务端的证书匹配

服务端验证客户端证书,首先把服务端的证书client.p12导入到服务端的密钥库里

然后在AFURLConnectionOperation.m中加入以下方法

-(OSStatus)extracentity :(CFDataRef)in p12 data :(SecIdentityRef *)identity {

OSStatus安全错误=errSecSuccess

CFStringRef密码=CFSTR('你的证书密码');

const void * keys[]={ ksecimportexportpassage };

const void * values[]={ password };

cf dictionary ref options=cf dictionary create(NULL,键,值,1,空,空);

CFArrayRef items=CFArrayCreate(NULL,0,0,NULL);

安全错误=secpkcs12导入(在p12数据、选项、项目中);

if (securityError==0)

{

cf dictionary ref ident=CFArrayGetValueAtIndex(items,0);

const void *tempIdentity=空;

tempIdentity=cf dictionary get value(ident,ksecimportititemidentity);

* identity=(SecIdentityRef)tempIdentity;

}

    if (options) {

        CFRelease(options);

    }

    return securityError;

}

把AFURLConnectionOperation.m中的

- (void)connection:(NSURLConnection *)connection

willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

替换成

- (void)connection:(NSURLConnection *)connection

willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

                        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"];

                        NSLog(@"thePath===========%@",thePath);

                        NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];

                        CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;

                        SecIdentityRef identity = NULL;

                        // extract the ideneity from the certificate

                        [self extractIdentity :inPKCS12Data :&identity];

                        SecCertificateRef certificate = NULL;

                        SecIdentityCopyCertificate (identity, &certificate);

                        const void *certs[] = {certificate};

//                        CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);

                        // create a credential from the certificate and ideneity, then reply to the challenge with the credential

                        //NSLog(@"identity=========%@",identity);

                        NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:nil persistence:NSURLCredentialPersistencePermanent];

//           credential = [NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray*)certArray persistence:NSURLCredentialPersistencePermanent];

            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];

}

然后就可以进行双向认证了

关于如何理解AFNetWorking https双向认证就分享到这里了,希望

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/117729.html

(0)

相关推荐

  • 大数据复习

    技术大数据复习 大数据复习大数据复习知识点框架1、大数据概述:复习习题集上的例题即可2、Hadoop:注意单机安装和伪分布式安装的区别,以及Hadoop中块的概念及意义!3、HDFS:(1)名称节点的3

    礼包 2021年11月12日
  • debian如何修改apache2 https端口为11443

    技术debian如何修改apache2 https端口为11443本篇文章为大家展示了debian如何修改apache2 https端口为11443,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希

    攻略 2021年11月12日
  • react的列表渲染

    技术react的列表渲染 react的列表渲染因为做了几次了,然后都忘了列表渲染具体怎么写的了,所以自己写一个随笔记录一下,加深一下记忆。
    react的列表渲染,就是在你有一个数组数据需要对它进行遍历单

    礼包 2021年11月1日
  • 怎样使SQL用户只能看到自己拥有权限的库

    技术怎样使SQL用户只能看到自己拥有权限的库怎样使SQL用户只能看到自己拥有权限的库,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。以SQL Server 2

    攻略 2021年12月1日
  • 如何进行CM部署客户端配置失败异常的分析

    技术如何进行CM部署客户端配置失败异常的分析如何进行CM部署客户端配置失败异常的分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。温馨提示:如果使用电脑

    攻略 2021年11月18日
  • linux如何获取脚本名称(linux怎么获取某一行的信息)

    技术怎么写linux脚本提取满足某个条件的行这篇文章主要介绍“怎么写linux脚本提取满足某个条件的行”,在日常操作中,相信很多人在怎么写linux脚本提取满足某个条件的行问题上存在疑惑,小编查阅了各式资料,整理出简单好

    攻略 2021年12月23日