iOS - HTTPS

一、簡介


二、HTTPS與HTTP的區別

  • 這裏用兩張圖來介紹兩者的區別:
    • HTTP:當客戶端發送請求,那麼服務器會直接返回數據。

      HTTP.png
      • HTTPS:當客戶端第一次發送請求的時候,服務器會返回一個包含公鑰的受保護空間(也成爲證書),當我們發送請求的時候,公鑰會將請求加密再發送給服務器,服務器接到請求之後,用自帶的私鑰進行解密,如果正確再返回數據。這就是 HTTPS 的安全性所在。

        HTTPS.png


文/Mitchell(簡書作者)
原文鏈接:http://www.jianshu.com/p/4b5d2d47833d
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。

三、實例

#import "ViewController.h"
@interface ViewController ()<NSURLSessionDataDelegate>
@end
@implementation ViewController
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSURL *url = [NSURL URLWithString:@"https://kyfw.12306.cn/otn/leftTicket/init"];
//    NSURL *url = [NSURL URLWithString:@"https://www.apple.com/"];
//    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
    [task resume];
}
#pragma mark - NSURLSessionDataDelegate
/*
// 只要訪問的是HTTPS的路徑就會調用
// 該方法的作用就是處理服務器返回的證書, 需要在該方法中告訴系統是否需要安裝服務器返回的證書
// NSURLAuthenticationChallenge : 授權質問
//+ 受保護空間
//+ 服務器返回的證書類型
 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
//    NSLog(@"didReceiveChallenge");
//    NSLog(@"%@", challenge.protectionSpace.authenticationMethod);

    // 1.從服務器返回的受保護空間中拿到證書的類型
    // 2.判斷服務器返回的證書是否是服務器信任的
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSLog(@"是服務器信任的證書");
        // 3.根據服務器返回的受保護空間創建一個證書
//         void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *)
//         代理方法的completionHandler block接收兩個參數:
//         第一個參數: 代表如何處理證書
//         第二個參數: 代表需要處理哪個證書
        //創建證書
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 4.安裝證書   completionHandler(NSURLSessionAuthChallengeUseCredential , credential);    
    }
}
*/
 - (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
   //AFNetworking中的處理方式
    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    __block NSURLCredential *credential = nil;  
    //判斷服務器返回的證書是否是服務器信任的
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        /*disposition:如何處理證書
     NSURLSessionAuthChallengePerformDefaultHandling:默認方式處理
         NSURLSessionAuthChallengeUseCredential:使用指定的證書    NSURLSessionAuthChallengeCancelAuthenticationChallenge:取消請求
         */
        if (credential) {
            disposition = NSURLSessionAuthChallengeUseCredential;
        } else {
            disposition = NSURLSessionAuthChallengePerformDefaultHandling;
        }
    } else {
        disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
    }
    //安裝證書
    if (completionHandler) {
        completionHandler(disposition, credential);
    }
}
// 接收到服務器的響應
 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    NSLog(@"didReceiveResponse");
    completionHandler(NSURLSessionResponseAllow);
}
// 接收到服務器返回的數據
 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData");
}
// 請求完畢
 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"didCompleteWithError");
}


四、問題

  • 有時採用HTTPS 無法接受數據,是因爲蘋果將http使用的是TLS 1.2 SSL 加密請求數據,而服務器有的時候使用的還是TLS 1.1
  • 解決辦法:在 info.plist 中添加
  • <key>NSAppTransportSecurity</key><dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/></dict>


文/Mitchell(簡書作者)
原文鏈接:http://www.jianshu.com/p/4b5d2d47833d
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章