国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS開發 支持https請求以及ssl證書配置詳解

2020-07-26 02:57:02
字體:
來源:轉載
供稿:網友

IOS開發 支持https請求以及ssl證書配置詳解

前言:

眾所周知,蘋果有言,從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];

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐水县| 西乌珠穆沁旗| 龙泉市| 东兴市| 麻阳| 民丰县| 贵州省| 西城区| 南华县| 玉门市| 东台市| 洛宁县| 东方市| 岑巩县| 和政县| 临漳县| 启东市| 永和县| 通山县| 南昌市| 新晃| 舒城县| 禄劝| 嵊州市| 罗江县| 博客| 绥滨县| 慈利县| 连州市| 广元市| 永兴县| 抚松县| 同德县| 邹城市| 拉孜县| 黄陵县| 龙南县| 濮阳县| 昭通市| 梁河县| 新和县|