菜鸟教程小白 发表于 2022-12-13 01:45:41

ios - 使用 Alamofire 将 iOS 应用程序连接到在 localhost 上运行的 api 时证书无效


                                            <p><p>我正在尝试连接到在 localhost 上运行的 API,以便可以在 iOS 模拟器中测试我的应用程序。我来了</p>

<blockquote>
<p>NSLocalizedDescription=The certificate for this server is invalid.
      You might be connecting to a server that is pretending to be “127.0.0.1”
      which could put your confidential information at risk.,
      NSErrorFailingURLKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
      NSErrorFailingURLStringKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
      NSErrorClientCertificateStateKey=0</p>
</blockquote>

<p>我正在使用 <strong>Alamofire</strong>。 <a href="https://stackoverflow.com/questions/30163274/how-to-connect-localhost-with-invalid-certificate-using-alamofire" rel="noreferrer noopener nofollow">This similar question</a>没有帮助。与旧版本的 Alamofire 相比,它似乎已经过时了。</p>

<p>我的 info.plist 已经包含</p>

<pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
&lt;dict&gt;
    &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
    &lt;true/&gt;
&lt;/dict&gt;
</code></pre>

<p>如何暂时禁用证书要求,以便在 localhost 上测试我的应用程序?</p>

<p>这是我在更改服务器信任策略后的代码,如其中一个答案所建议的那样</p>

<p> ViewController :</p>

<pre><code>class TableViewController: UIViewController {
    let postClient = PostServiceClient.sharedInstance

    override func viewDidLoad() {
      super.viewDidLoad()
      postClient.getPosts()
    }
}
</code></pre>

<p>PostServiceClient:</p>

<pre><code>import Alamofire

class PostServiceClient {

    static let sharedInstance: PostServiceClient = PostServiceClient()

    var sessionManager : SessionManager!

    init() {
      let serverTrustPolicies: = [
            &#34;https://127.0.0.1:8000/&#34; : .disableEvaluation
      ]

      self.sessionManager =SessionManager(configuration: URLSessionConfiguration.default,
                                              serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
      )
    }
    static let url = URL.init(string: &#34;https://127.0.0.1:8000/post/&#34;)

    // Method to get posts from the wall
    func getPosts(){
      print(&#34;Getting posts with completion handler&#34;)
      var request = URLRequest(url: PostServiceClient.url!)
      request.httpMethod = &#34;GET&#34;
      self.sessionManager.request(request).responseJSON { (response) in
            guard response.result.isSuccess else {
                print(&#34;Error while getting posts: \(String(describing: response.result.error))&#34;)
                return
            }
            guard let responseJSON = response.result.value as? ,
                let results = responseJSON[&#34;results&#34;] as? [] else {
                print(&#34;Invalid response recieved from service&#34;)
                return
            }
            print(responseJSON)
      }

    }
}
</code></pre>

<p>这是我得到的完整输出:</p>

<blockquote>
<p>Getting posts with completion handler 2017-06-19 14:22:15.770616-0400
WallAppiOS []
nw_coretls_callback_handshake_message_block_invoke_3
tls_handshake_continue: [-9812] 2017-06-19 14:22:15.770
WallAppiOS NSURLSession/NSURLConnection HTTP load
failed (kCFStreamErrorDomainSSL, -9813) Error while getting posts:
Optional(Error Domain=NSURLErrorDomain Code=-1202 &#34;The certificate for
this server is invalid. You might be connecting to a server that is
pretending to be “127.0.0.1” which could put your confidential
information at risk.&#34; UserInfo={NSLocalizedDescription=The certificate
for this server is invalid. You might be connecting to a server that
is pretending to be “127.0.0.1” which could put your confidential
information at risk., NSLocalizedRecoverySuggestion=Would you like to
connect to the server anyway?, _kCFStreamErrorDomainKey=3,
NSUnderlyingError=0x7a3627c0 {Error Domain=kCFErrorDomainCFNetwork
Code=-1202 &#34;(null)&#34;
UserInfo={_kCFStreamPropertySSLClientCertificateState=0,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorCodeKey=-9813, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=,
kCFStreamPropertySSLPeerCertificates=(
      &#34;&#34; )}}, _kCFStreamErrorCodeKey=-9813, NSErrorFailingURLStringKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
NSErrorPeerCertificateChainKey=(
      &#34;&#34; ), NSErrorClientCertificateStateKey=0,
NSURLErrorFailingURLPeerTrustErrorKey=,
NSErrorFailingURLKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>})</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在此示例中,我使用 <code>serverTrustPolicyManager</code> 来处理与 ssl 未认证服务器的连接,我使用单例来处理我的应用程序中的所有连接,您必须声明 <code>sessionManager</code>正如 Alamofire github 页面所说的那样</p>

<blockquote>
<p>Make sure to keep a reference to the new SessionManager instance,
otherwise your requests will all get cancelled when your
sessionManager is deallocated.</p>
</blockquote>

<pre><code>    class exampleNetworkClient {

      static let sharedInstance: exampleNetworkClient = exampleNetworkClient()

      var sessionManager : SessionManager?

      init() {
            let serverTrustPolicies: = [
                &#34;https://127.0.0.1:8000&#34; : .disableEvaluation
            ]

            self.sessionManager =SessionManager(configuration: URLSessionConfiguration.default,
                                  serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
            )
      }

      static let portalUrl = URL.init(string:&#34;https://127.0.0.1:8000/exampleserviceUrl&#34;)

    func exampleMethod()
    {
      var request = URLRequest(url: iOSETKClient.portalUrl!)
      request.httpMethod = &#34;GET&#34;

      //Important Note that you need to use your custom session manager
      self.sessionManager!.request(request).responseString { (response) in
            ///...RESPONSE LOGIC...///

      }
    }
}
</code></pre>

<p>希望对你有帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 Alamofire 将 iOS 应用程序连接到在 localhost 上运行的 api 时证书无效,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44621671/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44621671/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 Alamofire 将 iOS 应用程序连接到在 localhost 上运行的 api 时证书无效