该服务器的证书无效。

14

我知道如果使用以下 nsurlconnectiondelegate,问题将得到解决

- connection:willSendRequestForAuthenticationChallenge: - connection:canAuthenticateAgainstProtectionSpace

但是我正在尝试使用

sendAsynchronousRequest:queue:completionHandler:

所以你没有获取回调。我查阅了苹果文档,它说:

如果需要身份验证才能下载请求,则必须将所需的凭据作为 URL 的一部分指定。如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续。

我无法弄清楚如何做到这一点。当我搜索时,我只找到了这个私有调用

+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;

有任何想法如何做到这一点吗?

以下是我收到的错误信息:

该服务器的证书无效。您可能正在连接到一个假冒“example.com”的服务器={NSErrorFailingURLStringKey=https://example.com/test/, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://example.com/test/, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk., NSUnderlyingError=0xa26c1c0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=

8个回答

11

您使用的Web服务器正在请求服务器信任身份验证,您需要正确响应适当的操作。您需要实现connection:willSendRequestForAuthenticationChallenge:代理方法并使用SecTrustRef进行身份验证。

更多信息可以在此处找到:- https://developer.apple.com/library/ios/technotes/tn2232/_index.html

以下是我修复错误的代码:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

    id<NSURLAuthenticationChallengeSender> sender = [challenge sender];

    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        SecTrustRef trust = [[challenge protectionSpace] serverTrust];

        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:trust];

            [sender useCredential:credential forAuthenticationChallenge:challenge];
    }
    else
    {
        [sender performDefaultHandlingForAuthenticationChallenge:challenge];
    }
}

7

试试这个。

按照下面所示的自定义会话配置启动您的会话:

let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
                                 delegate: self,
                                 delegateQueue: nil)

实现以下委托回调函数:
public func urlSession(_: URLSession, task _: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    guard let serverTrust = challenge.protectionSpace.serverTrust else {
        return completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
    }
    return completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
}

2
这个和将 Allow Arbitrary Loads 设置为 YES 的组合对我有效。 - Tanin
2
是的,我假设人们已经将其设置为“是”。但是确实需要启用任意负载。 - Bhanu Birani

5
如果你正在使用AFNetworking,你可以使用以下代码:

(只是一个临时的客户端解决方案!)

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;

1
@JayprakashDubey 好的,那就看一下被接受的答案。 - skywinder

5

你不能用当前的方式解决它

  • 要么降级使用CFNetworking来允许错误证书
  • 使用带有委托和未公开方法的NSConnection
  • 使用你找到的私有API

以上方法都不好。 CFNetwork现在可能对苹果公司还可以,但其他两种方法甚至无法通过App Store审核。

更好的方法:解决服务器问题。这是最简单和最干净的方法。


原来是呼叫被重定向到了导致这个问题的服务器上。通过直接指向服务器解决了这个问题。 - pa12
如果在生产环境中使用私有API,则在提交到应用商店时,应用将被拒绝。@Daij-Djan - Jayprakash Dubey
是的,这就是我写的,“CFNetwork现在对于苹果来说可能还可以,但其他两种方法甚至都不安全可用于App Store”。 - Daij-Djan

1
在我的情况下,这个错误是由于我的防火墙阻止了所需的url而发生的。在解除防火墙限制后,它正常工作了。

1
无法使用块方式解决此问题。您需要设置委托并实现身份验证挑战委托以绕过证书验证。最佳解决方案是创建正确的证书(确保它不是自签名),或者如果您可以接受,则将协议更改为HTTP。

0
在我的情况下,这个错误是由于我的系统日期引起的。它被设置为一个旧日期,而证书从那个旧日期开始就不再有效了。纠正日期后,它就可以工作了。

0

这是一个证书错误。您需要更改设置,以便您的程序/操作系统忽略证书,或将URL / 证书添加到受信任列表中。

抱歉,那是身份验证,证书是身份验证。我看了一下,找到了这篇文章。

不确定它是否会解决您的问题,但基本上它说明,他们不涵盖在文档中使用证书连接到网站的情况。

http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/


1
-1 不好的建议,你不能这样做:引用:“我收到了一份报告,有人因为使用这种方法而被拒绝了他的应用程序。所以你只能将其用于测试。” - Daij-Djan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接