【问题标题】:How to bypass HTTPS server request using swift如何使用 swift 绕过 HTTPS 服务器请求
【发布时间】:2019-03-28 09:58:39
【问题描述】:

我想使用 URLSession 绕过 HTTPS 服务器请求。我有一个客观的 c 代码,它运行良好。但是当我将此代码转换为 swift 并且它不起作用时。请检查并帮助我。我的代码做错了什么?

// Objective C Code
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

// Swift Code 
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    if let aTrust = challenge.protectionSpace.serverTrust {
        completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: aTrust))
    }
}

【问题讨论】:

标签: ios swift https server nsurlsession


【解决方案1】:

除了一般来说是个坏主意(如果此代码最终出现在运输应用程序中,您将遇到非常严重的安全漏洞),这还会破坏您通过代理发出请求、破坏身份验证等的能力。请只是不要这样做。

您可以从 Let's Encrypt 获得免费证书,因此从不有充分的理由使用自签名证书,除非您正在做的事情涉及未连接到 Internet 的自定义硬件,即使你是,禁用验证仍然不是正确的方法。

只需删除代码并安装免费证书。

【讨论】:

猜你喜欢
  • 2016-07-06
  • 1970-01-01
  • 2012-12-30
  • 2019-10-22
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多