我想在Swift 2.0 Alamofire庫中調試SSL問題。我使用的是自定義的Alamofire經理建立這樣一個自定義ServerTrustPolicy:查找完成聲明處理程序
static let manager: Manager = {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"localhost": .PinCertificates(
certificates: ServerTrustPolicy.certificatesInBundle(),
validateCertificateChain: false,
validateHost: false
)
// "localhost": .DisableEvaluation
]
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
return Alamofire.Manager(configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
}()
我都嘗試,.PinCertificates
和.DisableEvaluation
。無論把我同樣的錯誤
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."
UserInfo={
NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x60c00004d980>,
NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,
_kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9802,
NSErrorPeerCertificateChainKey=<CFArray 0x6060001498a0 [0x10bb3c7b0]>{
type = immutable, count = 1, values = (
0 : <cert(0x61600006ed80) s: localhost i: localhost>
)},
NSUnderlyingError=0x6040000ad750 {
Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)"
UserInfo={
_kCFStreamPropertySSLClientCertificateState=0,
kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x60c00004d980>,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9802,
_kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9802,
kCFStreamPropertySSLPeerCertificates=<CFArray 0x6060001498a0 [0x10bb3c7b0]>{
type = immutable, count = 1, values = (
0 : <cert(0x61600006ed80) s: localhost i: localhost>
)}
}
},
NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.,
NSErrorFailingURLKey=https://localhost:3000/auth/requestToken?auth_appId=d018ccd505db2cb1d5aacabb03fc2f3a,
NSErrorFailingURLStringKey=https://localhost:3000/auth/requestToken?auth_appId=d018ccd505db2cb1d5aacabb03fc2f3a,
NSErrorClientCertificateStateKey=0
}
我試圖做使用curl
curl --cacert ./ca.pem https://localhost:3000
我把範圍縮小到從其中completionHandler是beeing稱爲單一的方法,它的工作就好了請求。直到關閉被稱爲一切正常,所以我想知道這個completionHandler實際上做了什麼。
public func URLSession(
session: NSURLSession,
didReceiveChallenge challenge: NSURLAuthenticationChallenge,
completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void))
{
// some other stuff but
// everything fine so far
completionHandler(disposition, credential)
}
我覺得URLSession
方法從調度隊列或類似的東西叫。
所以我的問題:我如何找到作爲completionHandler
參數傳遞給URLSession
方法的閉包?
你應該能夠進入關閉狀態。或者只是檢查這個方法被調用的地方。 –
你的問題不是很清楚。你在使用自定義的'ServerTrustPolicy'嗎? Alamofire默認使用Apple的默認服務器信任評估。如果你可以更具體一些,幫助起來會容易得多。 – cnoon
我用更多信息更新了我的問題:) – iMoritz