AFNetworking 3.0 https請求忽略證書驗證并添加請求頭驗證

1、修改AFNetworking中修改源碼,在AFSecurityPolicy.m注釋掉這幾句
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust  
                  forDomain:(NSString *)domain  
{  
//    if (domain && self.allowInvalidCertificates && self.validatesDomainName && (self.SSLPinningMode == GMAFSSLPinningModeNone || [self.pinnedCertificates count] == 0)) {  
//        // https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html  
//        //  According to the docs, you should only trust your provided certs for evaluation.  
//        //  Pinned certificates are added to the trust. Without pinned certificates,  
//        //  there is nothing to evaluate against.  
//        //  
//        //  From Apple Docs:  
//        //          "Do not implicitly trust self-signed certificates as anchors (kSecTrustOptionImplicitAnchors).  
//        //           Instead, add your own (self-signed) CA certificate to the list of trusted anchors."  
//        NSLog(@"In order to validate a domain name for self signed certificates, you MUST use pinning.");  
//        return NO;  
//    }  

或者可以添加一個宏開關(guān)“openHttpsSSL”,便于控制,類似如下代碼

- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                  forDomain:(NSString *)domain
{
    if (openHttpsSSL && domain && self.allowInvalidCertificates && self.validatesDomainName && (self.SSLPinningMode == AFSSLPinningModeNone || [self.pinnedCertificates count] == 0)) {
        // https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html
        //  According to the docs, you should only trust your provided certs for evaluation.
        //  Pinned certificates are added to the trust. Without pinned certificates,
        //  there is nothing to evaluate against.
        //
        //  From Apple Docs:
        //          "Do not implicitly trust self-signed certificates as anchors (kSecTrustOptionImplicitAnchors).
        //           Instead, add your own (self-signed) CA certificate to the list of trusted anchors."
        NSLog(@"In order to validate a domain name for self signed certificates, you MUST use pinning.");
        return NO;
    }

2、在使用的時候添加

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
            manager.requestSerializer = [AFJSONRequestSerializer serializer];  
  
            AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];  
            // allowInvalidCertificates 是否允許無效證書(也就是自建的證書),默認為NO  
            // 如果是需要驗證自建證書,需要設(shè)置為YES  
            securityPolicy.allowInvalidCertificates = YES;  
            manager.securityPolicy = securityPolicy;  

3、如果需要在http 頭里面添加用戶名和密碼驗證,添加

[request.operationManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"用戶名" password:@"密碼"];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢閱讀 99,586評論 9 467
  • 本想在這篇文章中單獨寫AFNetworking 3.0中AFSecurityPolicy的源碼閱讀筆記的。但隨著源...
    WeiHing閱讀 2,609評論 1 13
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 母親節(jié)臨不覺暖,思娘天國心卻寒。一片孝心何處訴,悼念摯親淚水漣。陰陽界,兩重天,腦海浮現(xiàn)舊堂前。寬厚仁慈音容貌,母...
    昀臻之實踐閱讀 192評論 0 0
  • 雙金棕櫚大佬把一段本該嚴肅沉重,蕩氣回腸的歷史拍得清湯寡水。這可能是大部分了解這段歷史觀眾的觀影感受。電影有兩處特...
    雨叮咚0閱讀 498評論 0 1