国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS開發(fā) 支持https請(qǐng)求以及ssl證書配置詳解

2019-10-21 18:48:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

IOS開發(fā) 支持https請(qǐng)求以及ssl證書配置詳解

前言:

眾所周知,蘋果有言,從2017年開始,將屏蔽http的資源,強(qiáng)推https

樓主正好近日將http轉(zhuǎn)為https,給還沒(méi)動(dòng)手的朋友分享一二

一、證書準(zhǔn)備

1、證書轉(zhuǎn)換

在服務(wù)器人員,給你發(fā)送的crt證書后,進(jìn)到證書路徑,執(zhí)行下面語(yǔ)句

// openssl x509 -in 你的證書.crt -out 你的證書.cer -outform der

這樣你就可以得到cer類型的證書了。雙擊,導(dǎo)入電腦。

2、證書放入工程

1、可以直接把轉(zhuǎn)換好的cer文件拖動(dòng)到工程中。

2、可以在鑰匙串內(nèi),找到你導(dǎo)入的證書,單擊右鍵,導(dǎo)出項(xiàng)目,就可以導(dǎo)出.cer文件的證書了

二、代碼準(zhǔn)備

<key>NSAppTransportSecurity</key>  <dict>    <key>NSAllowsArbitraryLoads</key>    <true/>  </dict>

1.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ù)器,客戶端信任證書    // 創(chuàng)建憑據(jù)對(duì)象    NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];    // 告訴服務(wù)器信任證書    [challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];  }}

你只需要簡(jiǎn)單的,添加上如上的代理方法,就可以在不影響你原有請(qǐng)求的基礎(chǔ)上,增加了https請(qǐng)求的支持了。

1.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ù)器證書  if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {    // 告訴服務(wù)器,客戶端信任證書    // 創(chuàng)建憑據(jù)對(duì)象    NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];    // 通過(guò)completionHandler告訴服務(wù)器信任證書    completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);  }  NSLog(@"protectionSpace = %@",challenge.protectionSpace);}

2.使用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)非常輕松.。

2.1 AFNetWorking 2.x版本

考慮到這個(gè)版本,我們還可以使用AFHTTPRequestOperationManager這個(gè)類來(lái)處理網(wǎng)絡(luò)請(qǐng)求。所以我們要做的就是給這個(gè)類,設(shè)置一些參數(shù),讓它可以支持https的請(qǐng)求,代碼如下:

支持https(校驗(yàn)證書,不可以抓包):

// 1.初始化單例類  AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];  mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;  // 2.設(shè)置證書模式  NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];  NSData * cerData = [NSData dataWithContentsOfFile:cerPath];  mgr.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:cerData, nil];  // 客戶端是否信任非法證書  mgr.securityPolicy.allowInvalidCertificates = YES;  // 是否在證書域字段中驗(yàn)證域名  [mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校驗(yàn)證書,可以抓包查看):

 // 1.初始化單例類  AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];  mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;  // 2.設(shè)置非校驗(yàn)證書模式  mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];  mgr.securityPolicy.allowInvalidCertificates = YES;  [mgr.securityPolicy setValidatesDomainName:NO];

2.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)證書,不可以抓包):

// 1.初始化   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;  // 2.設(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]];  // 客戶端是否信任非法證書  mgr.securityPolicy.allowInvalidCertificates = YES;  // 是否在證書域字段中驗(yàn)證域名  [mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校驗(yàn)證書,可以抓包查看):

// 1.初始化   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  // 2.設(shè)置非校驗(yàn)證書模式  manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];  manager.securityPolicy.allowInvalidCertificates = YES;  [manager.securityPolicy setValidatesDomainName:NO];

到這里配置就完成了,希望對(duì)你有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 保靖县| 黄平县| 汤原县| 芜湖市| 洪洞县| 上蔡县| 阆中市| 隆安县| 连州市| 抚州市| 拜泉县| 虞城县| 迁西县| 阜康市| 安顺市| 南川市| 宁津县| 安图县| 东港市| 吉木乃县| 乐亭县| 桑植县| 安平县| 宾阳县| 阿图什市| 洛川县| 山西省| 沁水县| 崇义县| 舞阳县| 大新县| 鱼台县| 伊吾县| 武清区| 会理县| 嘉定区| 金阳县| 化德县| 廉江市| 洛阳市| 余干县|