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

wordpress 视频压缩网站排名优化seo

wordpress 视频压缩,网站排名优化seo,重庆江津网站建设,wordpress 电商 开发NSURLSession 与 NSURLAuthenticationMethodServerTrust —— 从零开始的“服务器信任质询”全流程 目标读者#xff1a;刚接触 iOS 网络开发、准备理解 HTTPS 与证书校验细节的同学 出发点#xff1a;搞清楚为什么会有“质询”、质询的触发时机、以及在 delegate 里怎么正确…NSURLSession 与 NSURLAuthenticationMethodServerTrust —— 从零开始的“服务器信任质询”全流程 目标读者刚接触 iOS 网络开发、准备理解 HTTPS 与证书校验细节的同学 出发点搞清楚为什么会有“质询”、质询的触发时机、以及在 delegate 里怎么正确地处理它。 1. 质询到底是什么 当 URLSession 发现需要某种额外凭据credential才能继续网络交互时会暂停请求并向你抛出 authentication challenge。对 HTTPS 来说最常见的触发类型就是 NSURLAuthenticationMethodServerTrust 服务器把 X.509 证书链塞进 TLS 握手。 客户端iOS TLS 实现 ATS 默认策略检查 证书是否在有效期、是否被吊销证书链是否能追溯到系统或配置的受信根 CA证书的 CN/SAN 是否与请求的 host 完全匹配。 如果 全部 检查都能自动通过URLSession 不会打扰你——直接走默认证书校验并继续请求。 只要 你实现了 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- 及更早版本用 SecTrustEvaluateiOS 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)} }SecTrustSetAnchorCertificatesOnlytrue 的效果是“把系统根 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.protectionSpaceCode -1200 “SSL error”证书链无效 / ATS 阻止弱加密观察 Console中会打印 ATS policy requires...Charles 无法抓包ATS 拒绝了 Charles 证书或你启用了 Pinning临时将 NSExceptionDomains 加入 Info.plist或关闭 Pinning偶发 ServerTrust 失败服务器有多个证书链、SNI/host 不一致手动访问 https://host 用 openssl 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 看似只是“系统多问一句你到底信不信任这台服务器”但背后承载的是 PKI、TLS 乃至你 App 用户的数据安全。真正的安全措施都在“默认正确”与“最小权限” 默认让系统校验一切只有当你非常确定要改时才介入并且介入后要保证比系统 更严格 而不是更松。 掌握这些基础你就能轻松向自签环境、抓包调试甚至 SSL Pinning 过渡也能对任何“为什么连接被取消”作出快速诊断。愿你写出的每一行网络代码都能经得起安全审计与真实攻击的考验。
http://www.pierceye.com/news/769263/

相关文章:

  • 我们网站在那里登陆后台系统管理网站建设服务咨询
  • 免费上传图片的网址网站seo工作内容
  • chatgpt 网站一对一直播软件开发
  • 网站做排行多少费用个人电脑做网站打不开数据库
  • 做网站是比特币的滁州做网站电话号码
  • php网站开发说明怎么样建网站卖东西
  • 网站图片做多大浙江建设人才网
  • 网站关键词宝塔wordpress腾讯云
  • 优化排名推广教程网站免费房地产网站模板
  • 商城网站建设都需要多少钱电子商务网站建设预算
  • 万荣做网站怎么优化一个网站关键词
  • 潍坊市建设局网站网络工程师 网站建设
  • 做网站要求什么条件计算机网络技术学什么
  • 建设网站呼叫中心有什么好处中国能源建设集团有限公司级别
  • 免费论坛建站二 网站建设的重要性
  • wordpress站点迁移怎样做带音乐的表白网站
  • 海淀网站制作网站建设基本技术
  • 做一个平面网站的成本如何搭建一个app平台
  • 建设工程学部研究生培养网站义乌网站建设和制作
  • 简单的模板网站吉安网站建设jxthw
  • js做的网站佛山本地的网站设计公司
  • 企业网站页面网站建设朝阳
  • ui设计工具有哪些百度seo排名优化系统
  • 网站建设案例简介怎么写淘宝官方网站主页
  • 国外网站 dns南京模板做网站
  • 河北企业网站建设技术江西省外省建设入库网站
  • 网站建设的概念如何将自己做的网站放到网上去
  • 网站维护明细报价表最新的网站建设架构
  • 百度大全seo推广话术
  • 做网站赚钱流程英文网站建设注意什么