如何理解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)

相关推荐

  • React5种非常流行的状态管理库是什么

    技术React5种非常流行的状态管理库是什么本篇内容主要讲解“React 5种非常流行的状态管理库是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“React 5种非常流行

    攻略 2021年10月26日
  • 怎样使用Mfuzz进行时间序列表达模式聚类分析

    技术怎样使用Mfuzz进行时间序列表达模式聚类分析怎样使用Mfuzz进行时间序列表达模式聚类分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获

    攻略 2021年11月10日
  • OpenEuler基础实验

    技术OpenEuler基础实验 OpenEuler基础实验OpenEuler基础实验
    20191331 lyx
    基于 鲲鹏--OpenEuelr 20.03 64bit--ARM (华为云服务器)
    实验

    礼包 2021年11月1日
  • avue-crud遮罩层(avue-crud组件操作栏不显示)

    技术avue-crud多级复杂的动态表头怎么实现这篇文章主要讲解了“avue-crud多级复杂的动态表头怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“avue-c

    攻略 2021年12月16日
  • java如何使用java swing(swing 怎么启动一个后台线程)

    技术如何解析Swing线程的内容这篇文章将为大家详细讲解有关如何解析Swing线程的内容,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一. 事件1.事件来源:本地系统,应

    攻略 2021年12月18日
  • mobx报错

    技术mobx报错 mobx报错错误信息
    Support for the experimental syntax 'decorators-legacy' isn't currently enab
    安装mo

    礼包 2021年12月13日