iOS 配置https

貌似在iOS9之后,官方默认只能访问https。如果访问的https都是安全的,则不需要做任何配置。

 

 

安全的https都是通过官方认可的机构购买的SSL证书,如果是自己配置的证书则需要做一下配置了。

1、SSL证书

向后台开发者获取SSL证书(crt格式),并将该文件的格式转换成cer格式:

  • 方式一:个人试过,报错了
$ cd /Users/thbdsz/Desktop/证书/
$ openssl x509 -in m2.crt -out zj.cer -outform der

unable to load certificate
140735677465544:error:0906D06C:PEM routines:PEM_read_bio:no start line:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/pem/pem_lib.c:704:Expecting: TRUSTED CERTIFICATE
  • 方式二:双击该文件,在 keychain (钥匙串访问)中找到该文件的证书,然后导出cer文件。注意,完成后,在keychain中删除该证书。不然通过该电脑访问与该证书相同域名的url时,都需要配置该证书。

     

    ssl证书.png

2、AFN 3.0的访问

注意,这里老的的版本(具体什么版本不知道)可以不配置 kApiBaseUrl ,但是新的版本必须配置,并通过 kApiBaseUrl 创建 AFHTTPSessionManager

@interface NetManager : AFHTTPSessionManager

+ (instancetype)share;

@end

@implementation NetManager

+ (instancetype)share{
    static NetManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:kApiBaseUrl] sessionConfiguration:configuration];
        [manager setSecurityPolicy:[self customSecurityPolicy]];
    });
    
    return manager;
}

+ (AFSecurityPolicy *)customSecurityPolicy {
    
    // 先导入证书 证书由服务端生成,具体由服务端人员操作
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"m2" ofType:@"cer"];//证书的路径 xx.cer
    NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
    
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    // allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO
    // 如果是需要验证自建证书,需要设置为YES
    securityPolicy.allowInvalidCertificates = YES;
    
    //validatesDomainName 是否需要验证域名,默认为YES;
    //假如证书的域名与你请求的域名不一致,需把该项设置为NO;如设成NO的话,即服务器使用其他可信任机构颁发的证书,也可以建立连接,这个非常危险,建议打开。
    //置为NO,主要用于这种情况:客户端请求的是子域名,而证书上的是另外一个域名。因为SSL证书上的域名是独立的,假如证书上注册的域名是www.google.com,那么mail.google.com是无法验证通过的;当然,有钱可以注册通配符的域名*.google.com,但这个还是比较贵的。
    //如置为NO,建议自己添加对应域名的校验逻辑。
    securityPolicy.validatesDomainName = NO;
    
    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];
    return securityPolicy;
}
@end

3、info.plist,配置白名单

<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>cer文件中查看</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

cer.png

 

不配置白名单,照样出错:

[BoringSSL] Function nw_protocol_boringssl_input_finished: line 1436 Peer disconnected during the middle of a handshake. Sending errSSLFatalAlert(-9802) alert
TIC TCP Conn Failed [1:0x608000361ec0]: 3:-9802 Err(-9802)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Task <B02837F8-D481-46BF-9A1B-2F76E68B1A04>.<1> HTTP load failed (error code: -1200 [3:-9802])
Task <B02837F8-D481-46BF-9A1B-2F76E68B1A04>.<1> finished with error - code: -1200
2018-06-01 16:15:07.873601+0800 Test[26837:1632515] Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."
...等等信息

4、SDWebImage

使用下面方法即可。

[imv sd_setImageWithURL:[NSURL URLWithString:@"https://xxxxx:8080/img/upload/xxx.png"] placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];

5、WKWebView

设置代理,并实现下面的方法。

_webView.navigationDelegate = self;

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential *card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
    }
}

6、其他

如果项目中因为某些原因不全是https环境,例如分享功能,也需要添加白名单。
详情可看mob上说的适配iOS9.0
如果项目中还需要使用不知道域名为啥的http,则修改info.plist

<key>NSAllowsArbitraryLoads</key>
<true/>



作者:小蚊子叮迎行
链接:https://www.jianshu.com/p/8c128d9c9681
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值