最近調(diào)試SDWebImage加載自簽的https圖片時(shí)怎么也無(wú)法繞過(guò)驗(yàn)證苞俘。
換個(gè)思路,將自簽證書(shū)導(dǎo)入到SDWebimage使用的URLSession中龄章。
具體方法如下:
在SDWebimageDownloader.m
中添加URLSession代理方法
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
NSLog(@"證書(shū)認(rèn)證");
if ([[[challenge protectionSpace] authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust]) {
do{
SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
NSCAssert(serverTrust != nil, @"serverTrust is nil");
if(nil == serverTrust) break;
/* failed */
/**
* 導(dǎo)入多張CA證書(shū)(Certification Authority吃谣,支持SSL證書(shū)以及自簽名的CA),請(qǐng)?zhí)鎿Q掉你的證書(shū)名稱
*/
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"證書(shū)名字_cerName" ofType:@"cer"];
//自簽名證書(shū)
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 */
//可以添加多張證書(shū)
NSArray *caArray = @[(__bridge id)(caRef)];
NSCAssert(caArray != nil, @"caArray is nil");
if(nil == caArray) break;
/* failed */
//將讀取的證書(shū)設(shè)置為服務(wù)端幀數(shù)的根證書(shū)
OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);
NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");
if(!(errSecSuccess == status)) break;
/* failed */
SecTrustResultType result = -1;
//通過(guò)本地導(dǎo)入的證書(shū)來(lái)驗(yàn)證服務(wù)器的證書(shū)是否可信
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(@"信任該證書(shū)");
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];
}
在 證書(shū)名字_cerName
的位置填上自簽證書(shū)的名字做裙,即可將證書(shū)導(dǎo)入到SDWebimage的請(qǐng)求中岗憋,也可以添加多張證書(shū)。