iOS開發 支持https請求以及ssl證書配置(轉)

原文地址:http://blog.5ibc.net/p/100221.htmlhtml

 

衆所周知,蘋果有言,從2017年開始,將屏蔽http的資源,強推https
樓主正好近日將http轉爲https,給還沒動手的朋友分享一二api

1、證書準備 一、證書轉換服務器

在服務器人員,給你發送的crt證書後,進到證書路徑,執行下面語句
// openssl x509 -in 你的證書.crt -out 你的證書.cer -outform der
這樣你就能夠獲得cer類型的證書了。雙擊,導入電腦。網絡

二、證書放入工程session

一、能夠直接把轉換好的cer文件拖動到工程中。
二、能夠在鑰匙串內,找到你導入的證書,單擊右鍵,導出項目,就能夠導出.cer文件的證書了架構

2、代碼準備模塊化

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

1.1 NSURLConnection設置支持https。.net

在2015年iOS9的更新中,NSURLConnection 被廢棄 由 NSURLSession 取代,因此自己是不建議你們繼續用這個類作網絡請求的(一樣也有AFNetWorking 2.x版本),可是考慮到一些舊程序,也不能說改就改,說替換就替換的,因此仍是須要普及一下,若是用到了NSURLConnection你須要怎麼作。代理

代碼以下:code

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        // 告訴服務器,客戶端信任證書
        // 建立憑據對象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 告訴服務器信任證書
        [challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
    }
}

你只須要簡單的,添加上如上的代理方法,就能夠在不影響你原有請求的基礎上,增長了https請求的支持了。

1.2 NSURLSession設置支持https。

如今推薦使用的就是NSURLSession來處理相關的網絡請求了,若是使用系統自帶的類,能夠參考以下代碼:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task  didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
    // 判斷是不是信任服務器證書
    if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        // 告訴服務器,客戶端信任證書
        // 建立憑據對象
        NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 經過completionHandler告訴服務器信任證書
        completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
    }
    NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}

2.使用AFNetWorking發送網絡請求篇

AFNetworking是一個討人喜歡的網絡庫,適用於iOS以及Mac OS X. 它構建於在NSURLConnection, NSOperation, 以及其餘熟悉的Foundation技術之上. 它擁有良好的架構,豐富的api,以及模塊化構建方式,使得使用起來很是輕鬆.。

2.1 AFNetWorking 2.x版本

考慮到這個版本,咱們還可使用AFHTTPRequestOperationManager這個類來處理網絡請求。因此咱們要作的就是給這個類,設置一些參數,讓它能夠支持https的請求,代碼以下:
支持https(校驗證書,不能夠抓包):

// 1.初始化單例類
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
    // 2.設置證書模式
    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.設置非校驗證書模式
    mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    mgr.securityPolicy.allowInvalidCertificates = YES;
    [mgr.securityPolicy setValidatesDomainName:NO];

2.2 AFNetWorking 3.x版本

在Xcode7.0以後,蘋果廢棄了NSURLConnection方法,數據請求使用NSURLSession,做爲網絡請求類第三方庫使用量最大的AFN也及時的更新的新的版本——AFN 3.0版本。新的版本的裏廢棄了基於NSURLConnection封裝的AFHTTPRequestOperationManager,轉而使用基於NSURLSession封裝的AFHTTPSessionManager了。
支持https(校驗證書,不能夠抓包):

// 1.初始化
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
    // 2.設置證書模式
    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;
    // 是否在證書域字段中驗證域名
    [mgr.securityPolicy setValidatesDomainName:NO];

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

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

到這裏配置就完成了,但願對你有所幫助。

相關文章
相關標籤/搜索