【发布时间】:2021-09-15 16:04:24
【问题描述】:
我目前能够在我的 IOS 应用上使用 Alamofire 和 SSL pinning 登录我的公司网站。
但我无法登录我网站的子域。 我的代码中是否缺少任何特殊配置才能与我的子域建立 SSL 通信?
- 我在 app bundle 中添加了证书文件 .cer
- 创建 [SecCertificate]
func loadcertificate()->[SecCertificate]{
guard let pathToCert = Bundle.main.path(forResource: "amua", ofType: "cer") else {fatalError("can not find")}
guard let localCertificate = NSData(contentsOfFile: pathToCert) else {fatalError("can not load")}
guard let cert = SecCertificateCreateWithData(nil, localCertificate) else {fatalError("can not read cert")}
return [cert]
}
- 创建 Alamofire 会话和连接请求:
func connection() {
sessionManager = Session(configuration: URLSessionConfiguration.default)
let evaluator = PinnedCertificatesTrustEvaluator(certificates: loadcertificate(),
acceptSelfSignedCertificates: false,
performDefaultValidation: true,
validateHost: true)
let ServerTrustManager = ServerTrustManager(allHostsMustBeEvaluated: false,
evaluators:
["airmacau.com.mo" : evaluator])
sessionManager = Session(configuration: URLSessionConfiguration.default, delegate: SessionDelegate(), serverTrustManager: ServerTrustManager)
sessionManager?.request("https://icrew.airmacau.com.mo", method: .get, encoding: URLEncoding.default)
.response { response in
if let st = response.data {
let str = String(decoding: st, as: UTF8.self)
do {
print("OK")
let doc: Document = try SwiftSoup.parse(str)
print(doc)
}catch let err {
print("ERRORE .get icrew")
print(err.localizedDescription)
}
}
}
}
}
如果我连接到主网站地址https://www.airmacau.com.mo 一切正常,如果我连接到子域https://icrew.airmacau.com.mo 连接失败,我收到错误HANDSHAKE_FAILURE
【问题讨论】:
标签: swift xcode networking alamofire nsurlsession