欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年02月10日 08:54:40   作者:iOS_yanmy  
這篇文章主要介紹了IOS開發(fā) 支持https請(qǐng)求以及ssl證書配置詳解的相關(guān)資料,需要的朋友可以參考下

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

前言:

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

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

一、證書準(zhǔn)備

1、證書轉(zhuǎn)換

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

// 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版本),但是考慮到一些舊程序,也不能說改就改,說替換就替換的,所以還是需要普及一下,如果用到了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來處理相關(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];
    // 通過completionHandler告訴服務(wù)器信任證書
    completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
  }
  NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}

2.使用AFNetWorking發(fā)送網(wǎng)絡(luò)請(qǐng)求篇

AFNetworking是一個(gè)討人喜歡的網(wǎng)絡(luò)庫,適用于iOS以及Mac OS X. 它構(gòu)建于在NSURLConnection, NSOperation, 以及其他熟悉的Foundation技術(shù)之上. 它擁有良好的架構(gòu),豐富的api,以及模塊化構(gòu)建方式,使得使用起來非常輕松.。

2.1 AFNetWorking 2.x版本

考慮到這個(gè)版本,我們還可以使用AFHTTPRequestOperationManager這個(gè)類來處理網(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)求類第三方庫使用量最大的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)文章

最新評(píng)論