SDWebImage导入自签证书加载https图片

最近调试SDWebImage加载自签的https图片时怎么也无法绕过验证。

换个思路,将自签证书导入到SDWebimage使用的URLSession中。

具体方法如下:

SDWebimageDownloader.m中添加URLSession代理方法

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
NSLog(@"证书认证");
if ([[[challenge protectionSpace] authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust]) {
do{
    SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
    NSCAssert(serverTrust != nil, @"serverTrust is nil");
    if(nil == serverTrust)                break;
    /* failed */
    /**
     *  导入多张CA证书(Certification Authority,支持SSL证书以及自签名的CA),请替换掉你的证书名称
     */
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"证书名字_cerName" ofType:@"cer"];
    //自签名证书
    NSData* caCert = [NSData dataWithContentsOfFile:cerPath];

    NSCAssert(caCert != nil, @"caCert is nil");
    if(nil == caCert)                break;
    /* failed */
    SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);
    NSCAssert(caRef != nil, @"caRef is nil");
    if(nil == caRef)                break;
    /* failed */
    //可以添加多张证书
    NSArray *caArray = @[(__bridge id)(caRef)];
    NSCAssert(caArray != nil, @"caArray is nil");
    if(nil == caArray)                break;
    /* failed */
    //将读取的证书设置为服务端帧数的根证书
    OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);
    NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");
    if(!(errSecSuccess == status))                break;
    /* failed */
    SecTrustResultType result = -1;
    //通过本地导入的证书来验证服务器的证书是否可信
    status = SecTrustEvaluate(serverTrust, &result);
    if(!(errSecSuccess == status))                break;
    /* failed */
    NSLog(@"stutas:%d",(int)status);
    NSLog(@"Result: %d", result);
    BOOL allowConnect = (result == kSecTrustResultUnspecified) || (result == kSecTrustResultProceed);
    if (allowConnect) {
        NSLog(@"success");
    }else {
        NSLog(@"error");
    }

    /* kSecTrustResultUnspecified and kSecTrustResultProceed are success */
    if(! allowConnect)            {
        break;
        /* failed */
    }
    #if 0
    /*
     Treat kSecTrustResultConfirm and kSecTrustResultRecoverableTrustFailure as success 
     */

    /*   since the user will likely tap-through to see the dancing bunnies */
    if(result == kSecTrustResultDeny || result == kSecTrustResultFatalTrustFailure || result == kSecTrustResultOtherError)                break;
    /* failed to trust cert (good in this case) */
    #endif
    // The only good exit point
    NSLog(@"信任该证书");
    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,credential);

    return [[challenge sender] useCredential: credential forAuthenticationChallenge: challenge
];
}while(0);
}
// Bad dog
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,credential);

return [[challenge sender] cancelAuthenticationChallenge: challenge];
}

证书名字_cerName 的位置填上自签证书的名字,即可将证书导入到SDWebimage的请求中,也可以添加多张证书。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求,否则无法上架,因此研究了一下在iOS...
    VincentHK阅读 826评论 0 6
  • 前不久做了一个生成快照的需求,其中用到 SDWebImage 来下载图片,在使用该框架的过程中也遇到了一些问题,索...
    ShannonChenCHN阅读 14,136评论 12 241
  • 这个世界充满假象,唯有痛苦从不说谎…… 有人说,背上行囊,就是过客;放下包袱,就找到了故乡。其实我们每个人都明白,...
    永远的浩子阅读 144评论 0 1
  • 已宅一天 半天览文,半天打游戏,无闲做饭 我饿 夜深,天亮去买鸡腿肉,做盐酥鸡 我要吃个够
    甲骨文輸入法阅读 437评论 0 0
  • 步入假期已经好多天了,自己除了每天的贪睡玩手机外,就是做些简单的家务活,本来计划好的减肥,学习,但还是随着惬意...
    胖胖胖胖胖的不忘阅读 313评论 0 0