iOS APP配置HTTPS流程

2017年1月1日"蘋果iOS強(qiáng)制要求HTTPS連接",來提高數(shù)據(jù)傳輸之間的安全性,雖然之后又把時間推遲至待定,但支持HTTPS是遲早的事,是否支持HTTPS直接影響APP能否在蘋果商店順利上架。

本文只講解iOS APP端支持https的操作流程

  • 首先

info.plist中,增加App Transport Security SettingsAllow Arbitrary Loads,并把Allow Arbitrary Loadsvalue設(shè)置為YES,如圖:

截圖
  • NSURLConnection設(shè)置支持https

在iOS 9 的更新中,NSURLConnection被廢棄由NSURLSession 取代,所以本身是不建議大家繼續(xù)用這個類做網(wǎng)絡(luò)請求的(同樣也有AFNetWorking 2.x版本),但是考慮到一些舊程序,也不能說改就改,說替換就替換的,所以還是需要普及一下,如果用到了NSURLConnection你需要怎么做。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        // 告訴服務(wù)器,客戶端信任證書
        // 創(chuàng)建憑據(jù)對象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 告訴服務(wù)器信任證書
        [challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
    }
}
  • NSURLSession設(shè)置支持https

現(xiàn)在推薦使用的就是NSURLSession來處理相關(guān)的網(wǎng)絡(luò)請求了,如果使用系統(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ù)對象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 通過completionHandler告訴服務(wù)器信任證書
        completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
    }
    NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}
  • 使用AFNetWorking 2.x版本設(shè)置支持https

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

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

  // 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;
    // 是否在證書域字段中驗證域名
    [mgr.securityPolicy setValidatesDomainName:NO];

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

 // 1.初始化單例類
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
    // 2.設(shè)置非校驗證書模式
    mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    mgr.securityPolicy.allowInvalidCertificates = YES;
    [mgr.securityPolicy setValidatesDomainName:NO];
  • 使用AFNetWorking 3.x版本設(shè)置支持https

在Xcode7.0之后,蘋果廢棄了NSURLConnection方法,數(shù)據(jù)請求使用NSURLSession,作為網(wǎng)絡(luò)請求類第三方庫使用量最大的AFN也及時的更新的新的版本——AFN 3.0版本。新的版本的里廢棄了基于NSURLConnection封裝的AFHTTPRequestOperationManager,轉(zhuǎn)而使用基于NSURLSession封裝的AFHTTPSessionManager了。

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

 // 1.初始化單例類
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 注意寫法的變化
      manager.securityPolicy=  [AFSecurityPolicy policyWithPinningMode: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]];
    // 客戶端是否信任非法證書
    manager.securityPolicy.allowInvalidCertificates = YES;
    // 是否在證書域字段中驗證域名
    [manager.securityPolicy setValidatesDomainName:NO];

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

 // 1.初始化單例類
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.設(shè)置非校驗證書模式
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容