简体   繁体   English

覆盖 sessionDidReceiveChallenge 方法以绕过 Alamofire5 中的服务器信任问题

[英]override sessionDidReceiveChallenge method to bypass server trust issue in Alamofire5

Hi all we were using alamofire 4.9.1 before and recently i have upgraded to Alamofire 5 and with that we are facing bellow error大家好,我们之前使用的是 alamofire 4.9.1,最近我升级到了 Alamofire 5,因此我们面临以下错误

The certificate for this server is invalid.此服务器的证书无效。 You might be connecting to a server that is pretending to be “XXX.XXX.XX.X” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,您可能正在连接到伪装成“XXX.XXX.XX.X”的服务器,这可能会使您的机密信息处于危险之中。" UserInfo={NSLocalizedRecoverySuggestion=您仍然想连接到服务器吗?,

In Alamofire 4.9.1, we had resolve same issue by override didReceive challenge method as bellow在 Alamofire 4.9.1 中,我们通过覆盖 didReceive 挑战方法解决了同样的问题,如下所示

delegate.sessionDidReceiveChallenge = { session, challenge in
               var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
               var credential: URLCredential?

               if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                   disposition = URLSession.AuthChallengeDisposition.useCredential
                   credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
               } else {
                   if challenge.previousFailureCount > 0 {
                       disposition = .cancelAuthenticationChallenge
                   } else {
                       credential = WebAPIManager.manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                       if credential != nil {
                           disposition = .useCredential
                       }
                   }
               }
               return (disposition, credential)
           }

I have the same situation, and I am trying to refactor and adapt to the new version.我也有同样的情况,正在努力重构适应新版本。 So far I am finding this code in swift 5: And I still haven't determined where I can get "URLSessionTask" and "URLAuthenticationChallenge" from.到目前为止,我在 swift 5 中找到了这段代码:而且我还没有确定我可以从哪里获得“URLSessionTask”和“URLAuthenticationChallenge”。 Code:代码:

let delegate = SessionDelegate()让委托 = SessionDelegate()

            let delegateQueue:OperationQueue = .init()
    
            delegateQueue.underlyingQueue = .global(qos: .background)
    
            let session = URLSession(configuration: URLSessionConfiguration.af.default, delegate: delegate, delegateQueue: delegateQueue)
    
            let manager = Alamofire.Session(session: session, delegate: SessionDelegate(), rootQueue: .global(qos: .background))
    
    
    
    manager.delegate.urlSession(session, task: <#T##URLSessionTask#>, didReceive: <#T##URLAuthenticationChallenge#>) { (<#URLSession.AuthChallengeDisposition#>, <#URLCredential?#>) in
        <#code#>
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM