首页 > 解决方案 > 如何在 ios 模拟器中禁用 ip https 验证?

问题描述

我正在使用 Ionic 4 创建一个 ios webview。我已经构建了 ios 平台,并且正在使用 iphone XR-12 模拟器在 xcode 上进行测试。我的应用程序使用自签名证书调用 iframe 到 ip https 测试。问题是ios不允许我访问这个ip,因为它是自签名的。我用 iframe 调用这个服务器,还使用从 httpclient ionic 访问它的 Web 服务。我无法访问服务器。

我已经尝试使用 NSAppTransportSecurity 添加异常

    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>MI IP</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <false/>
                <key>NSExceptionAllowInsecureHTTPSLoads</key>
                <false/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
                <false/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSRequiresCertificateTransparency</key>
                <false/>
            </dict>
        </dict>
    </dict>

但是在一个论坛中,我读到它不适用于 ip,并且我需要一个像 www.example.com 这样的域

所以我尝试修改主机文件,但我必须更改所有重定向,并且 css 和链接无法正常工作,所以我排除了编辑主机的可能性也尝试在模拟器中安装证书。我将证书拖到模拟器并安装它并在证书信任设置中激活它。但它不起作用

已经尝试在 AppDelegate.m 中添加此代码

@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES;
}

也尝试在 CDVWKWebViewEngine.m 中添加此代码

- (void)webView:(WKWebView *)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
                                   *)challenge completionHandler:(void (^)
                                                                  (NSURLSessionAuthChallengeDisposition
                                                                   disposition, NSURLCredential *credential))completionHandler {
    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
    completionHandler(NSURLSessionAuthChallengeUseCredential,
                      [NSURLCredential credentialForTrust:serverTrust]);
}

还要在 config.xml 中添加这个

<preference name="CordovaWebViewEngine" value="CDVUIWebViewEngine"/>

但没有任何效果

在模拟器 ios 的日志中

任务 <837DAB71-F2D7-46C1-A29A-D1AD6736493F>.<2> 加载失败并出现错误错误域=NSURLErrorDomain Code=-1202“此服务器的证书无效。您可能正在连接到伪装成“ IP WEB SITE”,这可能会使您的机密信息面临风险。” UserInfo={NSLocalizedRecoverySuggestion=您还是要连接到服务器吗?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=( "" ), NSErrorClientCertificateStateKey=0, NSErrorFailingURLKey= https://IP WEB:4430/domesticas3p/registro, NSErrorFailingURLStringKey= https: //网页IP/domesticas3p/registro, NSUnderlyingError=0x7fa1a8e15650 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9843, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843, kCFStreamPropertySSLPeerCer []

并且不要在 iframe 和调用 Web 服务的问题所在的地方显示任何内容

对不起我的英语,谢谢你的帮助:)

标签: iosionic-frameworkios-simulator

解决方案


我认为你的.plist文件有倒退。NSAllowsArbitraryLoads您首先通过设置为禁用 ATS true,然后为您的 IP 创建一个例外,该例外不允许来自该 IP 的不安全加载。

NSExceptionAllowsInsecureHTTPLoads还要注意正确的设置不应该NSExceptionAllowsInsecureHTTPSLoads

.plist通过以下方式更改您的文件:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>your.ip.or.domain</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

这将使 ATS 保持打开状态,并为您的单个 IP 或域禁用它。


推荐阅读