【问题标题】:Getting "The certificate for this server is invalid." on iPad when loading an image from Amazon S3 (HTTPS), but no errors on simulator获取“此服务器的证书无效。”从 Amazon S3 (HTTPS) 加载图像时在 iPad 上,但在模拟器上没有错误
【发布时间】:2013-08-16 19:00:21
【问题描述】:

我正在尝试显示存储在 Amazon S3 存储桶上的图像。 URL 类似于https://s3.amazon.com/..../test.jpg。每当我在 iPhone 模拟器上执行此操作时,图像都会正确显示。但是,如果我在实际设备本身上对其进行测试,我会不断得到:

Error Domain=NSURLErrorDomain Code=-1202 "这个证书 服务器无效。您可能正在连接的服务器是 假装是“s3.amazonaws.com”,这可能会泄露您的机密 有风险的信息。” UserInfo=0x20007030 {NSErrorFailingURLStringKey=https://s3.amazonaws.com/.../test.jpeg, NSLocalizedRecoverySuggestion=你想连接到服务器吗 反正?, NSErrorFailingURLKey=https://s3.amazonaws.com/.../test.jpeg, NSLocalizedDescription=此服务器的证书无效。你 可能正在连接到假装的服务器 “s3.amazonaws.com”可以将您的机密信息放在 risk., NSUnderlyingError=0x20014d40 "此服务器的证书 是无效的。您可能正在连接到假装的服务器 是“s3.amazonaws.com”,它可以将您的机密信息放在 风险。”,NSURLErrorFailingURLPeerTrustErrorKey=}

有什么想法吗?!

谢谢!

【问题讨论】:

  • 如果您尝试在网络浏览器中加载测试 URL 会发生什么?
  • 一切正常!

标签: amazon-s3 nsurlconnection amazon afnetworking


【解决方案1】:

我从 S3 收到相同的证书错误,发现将其添加到 NSURLConnectionDelegate 解决了问题:

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] &&
       [challenge.protectionSpace.host hasSuffix:@"example.com"])
   {
       // accept the certificate anyway
       [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
   }
   else
   {
       [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
   }
}

注意:您需要将“example.com”更改为您信任的域,或使用比“hasSuffix”更复杂的机制。

仅供参考 Apple Technote TN2232“HTTPS 服务器信任评估”详细介绍了证书被拒绝的原因以及如何处理它: https://developer.apple.com/library/ios/technotes/tn2232/_index.html

感谢 Gordon Henriksen 在https://stackoverflow.com/a/2033823/235229 上回答这个问题,但使用的是较旧的 api。

【讨论】:

  • 完美答案,你拯救了我的一天。
【解决方案2】:

您还可以检查设备日期(如果有人错误地/故意将其更改为未来),以防您拥有受信任的证书但仍然出现此错误。

【讨论】:

    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 2011-09-14
    • 2015-03-29
    相关资源
    最近更新 更多