当前位置: 首页 > wzjs >正文

win7 添加asp网站国外做免费网站的

win7 添加asp网站,国外做免费网站的,网站系统的软件和硬件接口,创建学校网站吗NSURLSession 与 NSURLAuthenticationMethodServerTrust —— 从零开始的“服务器信任质询”全流程 目标读者:刚接触 iOS 网络开发、准备理解 HTTPS 与证书校验细节的同学 出发点:搞清楚为什么会有“质询”、质询的触发时机、以及在 delegate 里怎么正确…

NSURLSession 与 NSURLAuthenticationMethodServerTrust —— 从零开始的“服务器信任质询”全流程

目标读者:刚接触 iOS 网络开发、准备理解 HTTPS 与证书校验细节的同学
出发点:搞清楚为什么会有“质询”、质询的触发时机、以及在 delegate 里怎么正确地处理它


1. 质询到底是什么?

URLSession 发现需要某种额外凭据(credential)才能继续网络交互时,会暂停请求并向你抛出 authentication challenge。对 HTTPS 来说,最常见的触发类型就是 NSURLAuthenticationMethodServerTrust

  1. 服务器把 X.509 证书链塞进 TLS 握手。

  2. 客户端(iOS TLS 实现 + ATS 默认策略)检查:

    • 证书是否在有效期、是否被吊销;
    • 证书链是否能追溯到系统或配置的受信根 CA;
    • 证书的 CN/SAN 是否与请求的 host 完全匹配。
  3. 如果 全部 检查都能自动通过,URLSession 不会打扰你——直接走默认证书校验并继续请求。

  4. 只要 你实现了 session-level delegate 方法
    urlSession(_:didReceive:completionHandler:),系统就会把步骤 2 的工作“交卷”给你——即使校验本来能自动通过。

🚩 所以:不实现该 delegate == 自动信任系统 CA + ATS 默认策略;实现 delegate == 你必须亲自裁定是否信任。


2. 质询出现的典型场景

场景为什么会收到质询?你通常怎么做?
生产环境,使用合法证书(Let’s Encrypt、GlobalSign…)你自己实现了 delegate,但只是想保留系统默认验证再次调用 SecTrustEvaluateWithError,通过则 .useCredential
内网/测试环境 使用自签名证书系统根证书链里找不到颁发者把自签根证书预装到 App Bundle 并做自定义信任
SSL Pinning(证书/公钥固定)你想缩短信任链,拒绝被“合法”但非预期的 CA 篡改手动比对二进制证书或公钥哈希,然后再决定是否信任
使用 HTTP 抓包工具 (Charles、mitmproxy)代理伪造服务器证书,除非你安装其证书为根 CA开发调试时允许 Charles 证书;上线包一定要拒绝

3. 基础实现(Swift 5+)

/// 在创建 URLSession 时指定 delegate,而不是用 URLSession.shared
let session = URLSession(configuration: .default,delegate: self,delegateQueue: nil)extension YourNetworkManager: URLSessionDelegate {/// 系统对“服务器信任”发起的质询都会走到这里func urlSession(_ session: URLSession,didReceive challenge: URLAuthenticationChallenge,completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,let serverTrust = challenge.protectionSpace.serverTrust else {// 交给系统默认处理(例如 HTTP Basic、客户端证书等)completionHandler(.performDefaultHandling, nil)return}// 1️⃣ 让系统再跑一次标准评估if SecTrustEvaluateWithError(serverTrust, nil) {let credential = URLCredential(trust: serverTrust)completionHandler(.useCredential, credential)   // 继续请求} else {completionHandler(.cancelAuthenticationChallenge, nil) // 终止}}
}

iOS 13- 及更早版本用 SecTrustEvaluate;iOS 13+ 强烈建议改用 SecTrustEvaluateWithError 以拿到 CFError 信息并避免阻塞 main thread。

Objective-C 版本(简化)

- (void)URLSession:(NSURLSession *)sessiondidReceiveChallenge:(NSURLAuthenticationChallenge *)challengecompletionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {SecTrustRef trust = challenge.protectionSpace.serverTrust;if (SecTrustEvaluateWithError(trust, NULL)) {NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];completionHandler(NSURLSessionAuthChallengeUseCredential, cred);} else {completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);}return;}completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}

4. 深入:自签名证书与 SSL Pinning

4.1 只信任 Bundle 中的根证书

if let certPath = Bundle.main.path(forResource: "myRootCA", ofType: "cer"),let certData = try? Data(contentsOf: URL(fileURLWithPath: certPath)),let rootCert = SecCertificateCreateWithData(nil, certData as CFData) {SecTrustSetAnchorCertificates(serverTrust, [rootCert] as CFArray)SecTrustSetAnchorCertificatesOnly(serverTrust, true)if SecTrustEvaluateWithError(serverTrust, nil) {completionHandler(.useCredential, URLCredential(trust: serverTrust))} else {completionHandler(.cancelAuthenticationChallenge, nil)}
}

SecTrustSetAnchorCertificatesOnly=true 的效果是“把系统根 CA 全部踢出,仅信任我给定的这一束证书”。

4.2 公钥 Pinning(效率更高,证书续期更灵活)

guard let serverTrust = challenge.protectionSpace.serverTrust else { ... }
guard SecTrustEvaluateWithError(serverTrust, nil) else { ... }let serverPublicKey = SecTrustCopyKey(serverTrust)!
let serverKeyData   = SecKeyCopyExternalRepresentation(serverPublicKey, nil)! as Data
let serverKeyHash   = SHA256(serverKeyData) // 自己写或 CryptoKitif pinnedHashes.contains(serverKeyHash) {completionHandler(.useCredential, URLCredential(trust: serverTrust))
} else {completionHandler(.cancelAuthenticationChallenge, nil)
}

5. 典型错误排查清单

现象根因快速定位
Code -999 “已取消”你在 delegate 里返回了 .cancelAuthenticationChallenge.rejectProtectionSpace打断点检查 challenge.protectionSpace
Code -1200 “SSL error”证书链无效 / ATS 阻止弱加密观察 Console,中会打印 ATS policy requires...
Charles 无法抓包ATS 拒绝了 Charles 证书;或你启用了 Pinning临时将 NSExceptionDomains 加入 Info.plist,或关闭 Pinning
偶发 ServerTrust 失败服务器有多个证书链、SNI/host 不一致手动访问 https://hostopenssl s_client -servername host -connect ip:443

6. 最佳实践速览

  • 不做弱校验。切勿直接 .useCredential 而不跑 SecTrustEvaluateWithError,那等同于“信任一切”,上线会被审核拒绝。
  • Pin 公钥而非整张证书,减少因证书续期频繁发版。
  • 按需配置 ATS。绝大多数生产 HTTPS 服务都可以满足 ATS 默认要求:TLS 1.2+、至少 RSA 2048 或 ECC 256 、SHA-256 签名。
  • 调试与上线严格隔离。把抓包例外、测试根证书全部写在 #if DEBUG ... #endif 分支中。

结语

NSURLAuthenticationMethodServerTrust 看似只是“系统多问一句,你到底信不信任这台服务器?”,但背后承载的是 PKITLS 乃至你 App 用户的数据安全。真正的安全措施都在“默认正确”与“最小权限”

默认让系统校验一切,只有当你非常确定要改时才介入,并且介入后要保证比系统 更严格 而不是更松。

掌握这些基础,你就能轻松向自签环境、抓包调试甚至 SSL Pinning 过渡,也能对任何“为什么连接被取消?”作出快速诊断。愿你写出的每一行网络代码都能经得起安全审计与真实攻击的考验。


文章转载自:

http://cv5cbY4c.tnypd.cn
http://Ilv0IC4W.tnypd.cn
http://A5joQ6rD.tnypd.cn
http://lrF7lefZ.tnypd.cn
http://znYmE1qL.tnypd.cn
http://ZtTeysZf.tnypd.cn
http://FX9DGdzu.tnypd.cn
http://3VhAqwoH.tnypd.cn
http://Z9rzFmr5.tnypd.cn
http://7BybNStS.tnypd.cn
http://e5dECXpR.tnypd.cn
http://bg7CJ74G.tnypd.cn
http://3aatDWnj.tnypd.cn
http://ZHwv8m3g.tnypd.cn
http://9IO3StnE.tnypd.cn
http://ZHypr19Q.tnypd.cn
http://DmmDYFqq.tnypd.cn
http://bKNInxcH.tnypd.cn
http://Fwihr1PU.tnypd.cn
http://iDPXwJQP.tnypd.cn
http://LBfJsy8k.tnypd.cn
http://mG06qIF5.tnypd.cn
http://cnz64Lhu.tnypd.cn
http://WLxmct1C.tnypd.cn
http://SiD8nLyb.tnypd.cn
http://op0uB8rs.tnypd.cn
http://BXl3qpEK.tnypd.cn
http://F1wu53ET.tnypd.cn
http://wj9RkkF8.tnypd.cn
http://ZPPqUjXH.tnypd.cn
http://www.dtcms.com/wzjs/713764.html

相关文章:

  • 做网站办的营业执照用交税吗百度指数数据分析平台
  • 团购网站优化wordpress文章内容
  • 青岛做外贸网站的公司简介网络营销工具包括
  • 项目网站开发js放的位置代理网址怎么用
  • 百度网盘怎么做网站uniapp商城源码
  • 安康市城市建设局网站做网站哪一家公司好
  • 网站建设培训基地文化馆网站建设
  • 普陀建设机械网站渭南网站开发
  • 域名注册服务商网站管理网页
  • 凯里网站建设广告推广平台有哪些
  • 建设心理网站网站建设工程师待遇
  • 网站排名软件 利搜360网站建设官网
  • 怎么做特色网站wordpress自动部署
  • 怎么做文化传播公司网站高校学生红色网站建设
  • 清远建设工程招投标网站可以做问卷的网站
  • 响应式网站适合用什么框架做中国建筑集团有限公司电话
  • 网站整体风格设计做a免费视频在线观看网站
  • 网站备案多少天皮肤自做头像的网站
  • 可信的移动网站建设网站别人做的上面有方正字体
  • 诺诚建设工程有限公司网站怎样申请网站注册
  • 只用django做网站列表网网站建设
  • 网站开发找哪家企业网站推广公司 知乎
  • 便捷的网站建设字体设计作品赏析
  • 做网站时怎样把文字放在中间怎样自己搭建一个做影视的网站
  • 天津协会网站建设长沙营销推广
  • 网站大学报名官网入口南通网站建设论文
  • 河北省城乡与建设厅网站做理财的网站
  • 用什么软件做楼盘微网站自我介绍ppt模板免费下载
  • 软件行业有哪些岗位关键词seo公司
  • 网络网站开发培训全网关键词优化公司哪家好