AFNetworking 绕过 https的ssl认证

1.注释掉AFSecurityPolicy文件中的下面 if语句代码

- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                  forDomain:(NSString *)domain
{
//    if (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;
//    }

    NSMutableArray *policies = [NSMutableArray array];
    if (self.validatesDomainName) {
        [policies addObject:(__bridge_transfer id)SecPolicyCreateSSL(true, (__bridge CFStringRef)domain)];
    } else {
        [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
    }

2.请求语名加 AFSecurityPolicy 内容

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
            manager.requestSerializer = [AFJSONRequestSerializer serializer];  
  
            AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];  
            // allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO  
            // 如果是需要验证自建证书,需要设置为YES  
            securityPolicy.allowInvalidCertificates = YES;  
            manager.securityPolicy = securityPolicy;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值