ios AFNetworking支持https請(qǐng)求
原創(chuàng) 2016年09月29日09:54:04
ios 9出來(lái)以后花嘶,蘋果推薦使用https協(xié)議墩崩,來(lái)提高數(shù)據(jù)傳輸之間的安全性,下面將介紹如何在工程里面配置辫红,讓AF支持https請(qǐng)求辞居。
一也糊,證書(shū)準(zhǔn)備
- 1.證書(shū)轉(zhuǎn)換
在服務(wù)器人員问裕,給你發(fā)送的crt證書(shū)后,進(jìn)到證書(shū)路徑颓遏,執(zhí)行下面語(yǔ)句
// openssl x509 -in你的證書(shū).crt -out你的證書(shū).cer -outform der
這樣你就可以得到CER類型的證書(shū)了徐矩。雙擊,導(dǎo)入電腦叁幢。 - 2.證書(shū)放入工程
1滤灯,可以直接把轉(zhuǎn)換好的文件拖動(dòng)到工程中
。2曼玩,可以在鑰匙串內(nèi)鳞骤,找到你導(dǎo)入的證書(shū),單擊右鍵黍判,導(dǎo)出項(xiàng)目豫尽,就可以導(dǎo)出.cer文件的證書(shū)了
二。代碼修改
- 1項(xiàng)先在的info.plist中顷帖,增加如下圖的配置
的plist文件配置
使用XML格式配置plist文件內(nèi)容如下:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
三美旧。使用系統(tǒng)類發(fā)送網(wǎng)絡(luò)請(qǐng)求篇
- 3.1 NSURLConnection設(shè)置支持https。
在2015年iOS9的更新中贬墩,NSURLConnection 被廢棄 由 NSURLSession 取代榴嗅,所以本身是不建議大家繼續(xù)用這個(gè)類做網(wǎng)絡(luò)請(qǐng)求的(同樣也有AFNetWorking 2.x版本),但是考慮到一些舊程序,也不能說(shuō)改就改陶舞,說(shuō)替換就替換的嗽测,所以還是需要普及一下,如果用到了NSURLConnection你需要怎么做吊说。
代碼如下:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告訴服務(wù)器论咏,客戶端信任證書(shū)
// 創(chuàng)建憑據(jù)對(duì)象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 告訴服務(wù)器信任證書(shū)
[challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
}
}
現(xiàn)在,你只需要加上以上代理方法颁井,就可有簡(jiǎn)單的使用HTTPS請(qǐng)求了厅贪。
- 3.2 NSURLSession設(shè)置支持https。
現(xiàn)在推薦使用的就是NSURLSession來(lái)處理相關(guān)的網(wǎng)絡(luò)請(qǐng)求了雅宾,如果使用系統(tǒng)自帶的類养涮,可以參考如下代碼:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
// 判斷是否是信任服務(wù)器證書(shū)
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告訴服務(wù)器,客戶端信任證書(shū)
// 創(chuàng)建憑據(jù)對(duì)象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 通過(guò)completionHandler告訴服務(wù)器信任證書(shū)
completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
}
NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}
四眉抬。使用AFNetWorking發(fā)送網(wǎng)絡(luò)請(qǐng)求
AFNetworking是一個(gè)討人喜歡的網(wǎng)絡(luò)庫(kù),適用于iOS以及Mac OS X. 它構(gòu)建于在NSURLConnection, NSOperation, 以及其他熟悉的Foundation技術(shù)之上. 它擁有良好的架構(gòu),豐富的api,以及模塊化構(gòu)建方式,使得使用起來(lái)非常輕松贯吓。
-
4.1 AFNetWorking 2.x版本
考慮到這個(gè)版本,我們還可以使用AFHTTPRequestOperationManager這個(gè)類來(lái)處理網(wǎng)絡(luò)請(qǐng)求蜀变。所以我們要做的就是給這個(gè)類悄谐,設(shè)置一些參數(shù),讓它可以支持https的請(qǐng)求库北,代碼如下:
// HTTPS需要校驗(yàn)證書(shū)爬舰,不可以被抓包
// 1.初始化單例類
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.設(shè)置證書(shū)模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
mgr.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:cerData, nil];
// 客戶端是否信任非法證書(shū)
mgr.securityPolicy.allowInvalidCertificates = YES;
// 是否在證書(shū)域字段中驗(yàn)證域名
[mgr.securityPolicy setValidatesDomainName:NO];
// https们陆,不校驗(yàn)證書(shū),可以抓包
// 1.初始化單例類
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.設(shè)置非校驗(yàn)證書(shū)模式
mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
mgr.securityPolicy.allowInvalidCertificates = YES;
[mgr.securityPolicy setValidatesDomainName:NO];
- 4.2 AFNetWorking 3.x版本
在Xcode7.0之后情屹,蘋果廢棄了NSURLConnection方法坪仇,數(shù)據(jù)請(qǐng)求使用NSURLSession,作為網(wǎng)絡(luò)請(qǐng)求類第三方庫(kù)使用量最大的AFN也及時(shí)更新的版本--AFN 3.0版本垃你。新版本的里廢棄了基于NSURLConnection的封裝的AFHTTPRequestOperationManager椅文,轉(zhuǎn)而使用基于NSURLSession封裝的AFHTTPSessionManager了。
// 支持https惜颇,校驗(yàn)證書(shū)皆刺,不支持抓包
// 1.初始化單例類
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.設(shè)置證書(shū)模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];
// 客戶端是否信任非法證書(shū)
mgr.securityPolicy.allowInvalidCertificates = YES;
// 是否在證書(shū)域字段中驗(yàn)證域名
[mgr.securityPolicy setValidatesDomainName:NO];
//支持HTTPS,不校驗(yàn)證書(shū)官还,可以抓包
// 1.初始化單例類
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.設(shè)置非校驗(yàn)證書(shū)模式
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager.securityPolicy setValidatesDomainName:NO];