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

來源:互聯網
上載者:User

標籤:ppt   參數   網路   添加   api   use   系統   蘋果   請求   

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

 

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

一、認證準備1、認證轉換

在伺服器人員,給你發送的crt認證後,進到憑證路徑,執行下面語句
// openssl x509 -in 你的認證.crt -out 你的認證.cer -outform der
這樣你就可以得到cer類型的認證了。雙擊,匯入電腦。

2、認證放入工程

1、可以直接把轉換好的cer檔案拖動到工程中。
2、可以在鑰匙串內,找到你匯入的認證,單擊右鍵,匯出項目,就可以匯出.cer檔案的認證了

二、代碼準備

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

1.1 NSURLConnection設定支援https。

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

代碼如下:

- (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];

到這裡配置就完成了,希望對你有所協助。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.