网络请求

iOS HTTPS自建证书验证

2018-08-24  本文已影响107人  HCong

前言

公司项目由HTTP转为HTTPS,需要对网络请求进行自建证书验证。主要是AFNetWorkingSDWebImageWKWebView

HTTP与HTTPS

详情参考iOS开发-网络、Http与Https

AFNetWorking

详情参考AFNetworking之于https认证。之后的主要是使用AFNetWorking的方法进行验证。

static AFSecurityPolicy *securityPolicyShare = NULL;
@implementation HTTPSAuthenticationChallenge

+(AFSecurityPolicy *)customSecurityPolicy {
    // 保证证书验证初始化一次
    if (securityPolicyShare != NULL) {
        return securityPolicyShare;
    }

    // 加载证书
    NSString *crtBundlePath = [[NSBundle mainBundle] pathForResource:@"Res" ofType:@"bundle"];
    NSBundle *resBundle = [NSBundle bundleWithPath:crtBundlePath];
    NSSet<NSData *> *cerDataSet = [AFSecurityPolicy certificatesInBundle:resBundle];
    
    // AFSSLPinningModeCertificate使用证书验证模式
    securityPolicyShare = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:cerDataSet];
    
    return securityPolicyShare;
    
}

+ (void)authenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    // 获取服务器证书信息
    SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
    
    NSURLCredential *credential = nil;
    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    
    // 基于客户端的安全策略来决定是否信任该服务器,不信任的话,也就没必要响应验证
    if ([[HTTPSAuthenticationChallenge customSecurityPolicy] evaluateServerTrust:serverTrust forDomain:nil]) {
        
        // 创建挑战证书(注:挑战方式为UseCredential和PerformDefaultHandling都需要新建证书)
        credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        
        // credential存在时使用证书验证
        // credential为nil时忽略证书,默认的处理方式
        disposition = credential == nil ?  NSURLSessionAuthChallengePerformDefaultHandling : NSURLSessionAuthChallengeUseCredential;
        
    } else {
        // 忽略证书,取消请求
        disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
    }
    
    
    if (completionHandler) {
        completionHandler(disposition,credential);
    }
}

@end

SDWebImage

SDWebImageDownloaderSDWebImageView下载图片的核心类,在分类中重写NSURLSession的代理方法didReceiveChallenge进行自建证书的验证

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge

 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
    
    [HTTPSAuthenticationChallenge authenticationChallenge:challenge completionHandler:completionHandler];
    
}

WKWebView

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
   [HTTPSAuthenticationChallenge authenticationChallenge:challenge completionHandler:completionHandler];
}
上一篇 下一篇

猜你喜欢

热点阅读