按照我的要求,我不應該讓允許任意加載=true。所以我設定為false。
而且我在我的URLsession委托上允許信任證書。
我的網址 : https://sample-app.10.names.io
代碼 :
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential? ) -> Void) {
//Trust the certificate even if not valid[/span].
let urlCredential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, urlCredential)
}
我的錯誤 :
Code=-1200 "發生了一個SSL錯誤,無法與服務器建立安全連接。" UserInfo={NSLocalizedRecoverySuggestion=無論如何你都要連接到服務器嗎?,
。如果我讓允許任意加載=true,那么它就能作業。但根據我的要求,我不應該改變為真。任何建議都是有幫助的。
謝謝
更新:
我也試過下面這個方法:
<key>/span>NSAppTransportSecurity</key>
<決定>
<key>NSExceptionDomains</key>
<決定>
<key>https://sample-app.10.names.io< /key>
<dict>
<!--Include以允許子域-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include以允許HTTP請求-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>。
<true/>
<!--Include指定最小TLS版本-->/span>
<key>NSTemporaryExceptionMinimumTLSVersion< /key>
<string>TLSv1. 1</string>。
</dict>/span>
</dict>/span>
</dict>/span>
uj5u.com熱心網友回復:
你可以使用終端命令
nscurl --ats-diagnostics --verbose https://sample-app.10.names.io
來測驗您的服務器是否符合ATS。
這樣做顯示您的服務器只有在完全前向保密被禁用時才能通過。 看來您的服務器不支持ECDHE密碼器。
您可以通過在ATS配置例外域中指定NSExceptionRequiresForwardSecrecy來配置ATS以忽略完美的前向保密性要求,但實際上您應該給您的服務器打補丁以使用更新的TLS代碼。 否則,它很容易受到重放式MITM攻擊。
uj5u.com熱心網友回復:
如果你在你的專案中積極使用Alamofire,我建議你使用內置的Session,它可以被配置為使用你的服務器信任證書而不需要太多的麻煩。下面是一些關于如何在我的一個專案中設定它的代碼。
class SessionManagerProvider {
// MARK: - State
let hosts: [String]
let disableEvaluation: Bool] let disableEvaluation.
// MARK: - Init
init(urls: [URL], disableEvaluation: Bool = false) {
hosts = urls.compactMap { $0.host }
self.disableEvaluation = disableEvaluation
}
// MARK: - Factory.
func make() -> Session {
//配置網路客戶端與SSL pinning。
let配置 = URLSessionConfiguration.af.default
配置.timeoutIntervalForRequest = Constants.Backend.timeoutIntervalForRequest
配置.timeoutIntervalForResource = Constants.Backend.timeoutIntervalForResource
//允許比API請求更多的連接,以避免出現問題,當URLSession開始。
///當有太多的連接時,請求超時。
configuration.httpMaximumConnectionsPerHost = Constants.maxConcurrentApiCalls * 2
let policies = serverTrustPolicies(disableEvaluation: disableEvaluation)
let securityManager = ServerTrustManager(evaluationators: policies)
let sessionManager = Session(configuration: 配置, serverTrustManager: securityManager)
return sessionManager。
}
private func serverTrustPolicies(disableEvaluation: Bool) -> [String: ServerTrustEvaluating]。{
var policies: [String: ServerTrustEvaluating]。= [ :]
for host in hosts {
if disableEvaluation {
policies[host] = DisabledTrustEvaluator()
} else {
policies[host] = PublicKeysTrustEvaluator(
performDefaultValidation: true,
validateHost: true, validateHost: true.
)
}
}
return 政策
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307580.html
標籤:
上一篇:<p>我有一些字串(CIGARs),我試圖將<code>"I"</code>前面的數字相加。<code>"I"&
