iOS ATS AFNetworking 單項認證

來源:互聯網
上載者:User

標籤:允許   子網域名稱   mod   串連   x509   reserve   無法   file   com   

 TLS:Transport Layer Security

首先TLS是有版本的,iOS要求的版本是 TLS 1.2.

為了以防萬一,首先還是檢查一下服務端提供的版本.

nscurl --ats-diagnostics --verbose https://app.xxx.com

比如這個就不支援..

 

然後就是AFNetworking的配置了.直接設定AFHTTPSessionManager的SecurityPolicy就行.

 

#import <Foundation/Foundation.h>@class AFSecurityPolicy;@interface NetSecurityPolicy : NSObject+(AFSecurityPolicy*)SecurityPolicy;@end////////////  NetSecurityPolicy.m//////  Created by KayCM on 2016/11/29.//  Copyright ? 2016年 M1989. All rights reserved.//#import "NetSecurityPolicy.h"#import "AFNetworking.h"@implementation NetSecurityPolicy+(AFSecurityPolicy*)SecurityPolicy{    // /先匯入認證    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"cer"];//認證的路徑        NSData *certData = [NSData dataWithContentsOfFile:cerPath];        if (certData){                // AFSSLPinningModeCertificate 使用認證驗證模式        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];                // allowInvalidCertificates 是否允許無效認證(也就是自建的認證),預設為NO        // 如果是需要驗證自建認證,需要設定為YES        securityPolicy.allowInvalidCertificates = NO;                //validatesDomainName 是否需要驗證網域名稱,預設為YES;        //假如認證的網域名稱與你請求的網域名稱不一致,需把該項設定為NO;如設成NO的話,即伺服器使用其他可信任機構頒發的認證,也可以建立串連,這個非常危險,建議開啟。        //置為NO,主要用於這種情況:用戶端請求的是子網域名稱,而認證上的是另外一個網域名稱。因為SSL認證上的網域名稱是獨立的,假如認證上註冊的網域名稱是www.google.com,那麼mail.google.com是無法驗證通過的;當然,有錢可以註冊萬用字元的網域名稱*.google.com,但這個還是比較貴的。        //如置為NO,建議自己添加對應網域名稱的校正邏輯。        securityPolicy.validatesDomainName = YES;                securityPolicy.pinnedCertificates =   [NSSet setWithObject:certData];//@[certData];                return securityPolicy;            }else{                NSLog(@"認證出錯");                return nil;    }                    }@end

 

調用

AFHTTPSessionManager *session = [AFHTTPSessionManager manager];    [session setSecurityPolicy:[NetSecurityPolicy SecurityPolicy]];

 

其他,pem轉cer

openssl x509 -in xxx.pem -outform der -out server.cer

 

iOS ATS AFNetworking 單項認證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.