3

我需要从 Alamofire 4 迁移到 5,但我缺少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 版,委托已更改为SessionDelegate类,但没有提供类似的功能

我尝试使用这样的委托URLSession

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 中正确管理请求挑战

4

2 回答 2

2

没有必要覆盖SessionDelegate使用客户端证书。Alamofire 将自动使用附加URLCredential的客户端证书挑战。只需将凭证附加到请求中:

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

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

于 2020-05-26T22:55:14.770 回答
0

URLProtectionSpace对于我在共享存储上使用的会话级别的证书处理URLCredentialStorage,然后将其设置为Alamofire.Session配置

这是一个设置它的示例(端口 443 可能就足够了)

fileprivate func registerURLCredential() {

    let storage = URLCredentialStorage.shared

    do {
        let credential: URLCredential = try loadURLCredential("certificate", password: "blablabla")

        let url = URL.API
        let host = url.host ?? ""

        let ports: [Int] = [80, 443, url.port ?? 0]

        for port in ports {

            let space = URLProtectionSpace(
                host: host,
                port: port,
                protocol: url.scheme,
                realm: nil,
                authenticationMethod: NSURLAuthenticationMethodClientCertificate
            )

            storage.set(credential, for: space)
        }
    } catch {
        print(error)
    }
}


fileprivate func createSession(_ configurationHandler: ((_ configuration: URLSessionConfiguration) -> Void)? = nil) -> Alamofire.Session {

    let configuration = URLSessionConfiguration.af.default

    registerURLCredential()

    configuration.urlCredentialStorage = .shared

    configurationHandler?(configuration)

    let session = Session(
        configuration: configuration,
        requestQueue: .global(qos: .background),
        serializationQueue: .global(qos: .background)
    )

    return session
}

一个简单的用途是:

let sesstion = createSession({ configuration in

    configuration.httpMaximumConnectionsPerHost = 1
})
于 2020-06-03T12:42:31.843 回答