因此,昨晚发布的iOS新版beta SDK中有一个名为“应用程序传输安全”的功能,鼓励开发人员使用https而不是http。原则上,这是一个很好的想法,我已经在我们的预发布/生产环境中使用https。然而,在我的本地开发环境中,当iOS应用程序连接到我在笔记本电脑上运行的Web服务时,我没有设置https。
从今天早上的一些实验中可以看出,URL加载系统即使你提供了http URL,也会决定使用https。有人知道如何禁用此行为-即使只针对特定的URL吗?
因此,昨晚发布的iOS新版beta SDK中有一个名为“应用程序传输安全”的功能,鼓励开发人员使用https而不是http。原则上,这是一个很好的想法,我已经在我们的预发布/生产环境中使用https。然而,在我的本地开发环境中,当iOS应用程序连接到我在笔记本电脑上运行的Web服务时,我没有设置https。
从今天早上的一些实验中可以看出,URL加载系统即使你提供了http URL,也会决定使用https。有人知道如何禁用此行为-即使只针对特定的URL吗?
请查看苹果的Info.plist参考文档以获取完整细节(感谢@gnasher729)。
您可以在Info.plist中为特定域名添加异常。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>testdomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
除了期望的域之外,每个键都是可选的。演讲者没有详细说明任何键,但我认为它们都相当显而易见。
(来源:WWDC 2015会议703,“隐私和您的应用程序”,30:18)
如果您的应用程序有充分理由这样做,您还可以使用单个键忽略所有应用程序传输安全限制:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
如果您的应用程序没有充分的理由,使用 NSAllowsArbitraryLoads 标志可能会导致被拒绝:
将 NSAllowsArbitraryLoads 设置为 True 可以让应用程序正常工作,但是苹果公司非常清楚地表示他们打算拒绝使用此标志的应用程序,除非有特定的原因。我能想到使用 NSAllowsArbitraryLoads 的主要原因可能是用户创建的内容(链接共享、自定义 Web 浏览器等)。即使在这种情况下,苹果仍然希望您包括强制 ATS 的例外情况,以针对您控制的 URL。
如果确实需要访问未通过 TLS 1.2 服务的特定 URL,则需要编写针对这些域名的特定例外条件,而不是使用 NSAllowsArbitraryLoads 设置为 Yes。您可以在 NSURLSesssion WWDC 会话中找到更多信息。
请注意谨慎在分享 NSAllowsArbitraryLoads 解决方案时。这不是苹果推荐的修复方法。
— kcharwood(感谢 @marco-tolman)
NSTemporaryExceptionAllowsInsecureHTTPLoads
,
NSTemporaryExceptionMinimumTLSVersion
,
NSTemporaryExceptionRequiresForwardSecrecy
,
NSTemporaryThirdPartyExceptionAllowsInsecureHTTPLoads
,
NSTemporaryThirdPartyExceptionMinimumTLSVersion
,
NSTemporaryThirdPartyExceptionRequiresForwardSecrecy
。 - adurdinNSIncludesSubdomains
和NSTemporaryExceptionAllowsInsecureHTTPLoads
设置为<true />
。 - Cœur既然采纳的答案提供了所需信息,关于使用和禁用应用程序传输安全性的更多信息,可以在此处找到。
对于每个域名的异常情况,请将以下内容添加到Info.plist中:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
但是如果我不知道我需要使用的所有不安全域名怎么办? 在您的Info.plist中使用以下键。
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
或者,您可以使用一个plist文件并排除特定的服务器。然而,似乎无法排除IPv4地址,因此您可能需要使用服务器名称(在系统偏好设置->共享中找到,或在本地DNS中配置)。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>server.local</key>
<dict/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
对我来说,上述配置并没有起作用。我尝试了很多键的组合,但只有这个有效:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>mydomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
根据@adurdin和@User提供的答案进行编译。
将以下内容添加到您的info.plist中,并将localhost.com
更改为相应的域名,您也可以添加多个域名:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
</plist>
你的 info.plist 必须像这样:
这是对我起作用的方法:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key><!-- your_remote_server.com / localhost --></key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
<!-- add more domain here -->
</dict>
</dict>
我只想添加一点内容来帮助他人并节省一些时间:
如果您使用的是:CFStreamCreatePairWithSocketToHost
。请确保您的host
与您在.plist
中拥有的相同,或者如果您有一个专门用于套接字的域,请将其添加到那里。
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)/*from .plist*/, (unsigned int)port, &readStream, &writeStream);
希望这对你有所帮助。祝好!:)