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

相关推荐

  • 位图索引BitMap举例分析

    技术位图索引BitMap举例分析这篇文章主要介绍“位图索引BitMap举例分析”,在日常操作中,相信很多人在位图索引BitMap举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”位图索

    攻略 2021年12月8日
  • 分析Oracle RAC集群

    技术分析Oracle RAC集群这篇文章主要讲解了“分析Oracle RAC集群”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“分析Oracle RAC集群”吧!一、什么是

    攻略 2021年11月4日
  • 如何在Cubieboard上搭建个人云存储

    技术怎么在Cubieboard上搭建个人云存储这篇文章将为大家详细讲解有关怎么在Cubieboard上搭建个人云存储,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。准备工作

    攻略 2021年12月13日
  • linux三剑客之grep 文本过滤器

    技术linux三剑客之grep 文本过滤器 linux三剑客之grep 文本过滤器Linux 三剑客之grep
    三剑客之一:grep 定义: 文本过滤器: 根据文本内容过滤文件。语法格式 :grep 参

    礼包 2021年12月21日
  • 刷抖音播放量,抖音在线刷播放量网址?

    技术刷抖音播放量,抖音在线刷播放量网址?抖音快车这个引流渠道目前是见效快的办法,目前抖音HOT热门位子很火,流量相比位的三分之一获取得到,所以各大商家纷纷竞争目前HOT位子是根据点击率+ROI进行评估的。做引流先做快车,

    测评 2021年10月26日
  • arctanx求导等于什么,arctanx的导数是什么

    技术arctanx求导等于什么,arctanx的导数是什么arctanx的导数arctanx求导等于什么:1/(1+x²)。证明:y=arctanx,x=tany,dx/dy=sec²y=tan²y+1,dy/dx=1/

    生活 2021年10月26日