gpt4 book ai didi

ios - Alamofire 5 委托(delegate)中缺少 sessionDidReceiveChallenge

转载 作者:行者123 更新时间:2023-12-01 16:12:13 25 4
gpt4 key购买 nike

我需要从 Alamofire 4 迁移到 5,但我缺少委托(delegate)上的 sessionDidReceiveChallenge 回调

我之前在版本 4 中使用过这样的东西:

let manager = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default
)

manager.delegate.sessionDidReceiveChallenge = { session, challenge in

let method = challenge.protectionSpace.authenticationMethod

if method == NSURLAuthenticationMethodClientCertificate {
return (.useCredential, self.cert.urlCredential())
}
if method == NSURLAuthenticationMethodServerTrust {
let trust = challenge.protectionSpace.serverTrust!
let credential = URLCredential(trust: trust)
return (.useCredential, credential)
}
return (.performDefaultHandling, Optional.none)
}

但现在是版本 5,委托(delegate)已更改为 SessionDelegate 类,但没有提供类似的功能

我尝试像这样使用来自 URLSession 的委托(delegate):

let delegate = SomeSessionDelegate()

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)
)

class SomeSessionDelegate: NSObject, URLSessionDelegate {

let cert = ...

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

//same impl as before
}
}

我猜我在第 5 版中的实现是错误的,因为我停止接收响应回调

请告知如何在版本 5 中正确管理请求质询

最佳答案

无需覆盖 SessionDelegate 即可使用客户端证书。 Alamofire 将自动使用附加的 URLCredential 进行客户端证书质询。只需将凭据附加到请求中即可:

AF.request(...)
.authenticate(with: clientCertCredential)
.response...

此外,您的服务器信任检查将返回任何有效的信任,这可能是一个安全问题。我会立即停止使用该代码。

关于ios - Alamofire 5 委托(delegate)中缺少 sessionDidReceiveChallenge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62024789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com