首页 > 解决方案 > 如何在颤振中通过自生成的签名证书进行 SSL 固定?

问题描述

我正在寻找 SSL pinning 并使用自生成的证书来运行我们的 api。

标签: flutter

解决方案


问题中没有足够的细节,所以这个答案是基于一些假设:

  1. 您的 API 是 HTTPS
  2. 您正在谈论验证服务器端自签名 HTTPS 证书
  3. 您正在package:http用作 http 客户端
  4. 没有客户端证书

package:httpdart:io HttpClient在引擎盖下使用,并HttpClient具有多个允许证书验证的功能。由于客户端不信任自签名服务器证书,因此客户端将调用badCertificateCallback允许您自己验证服务器证书,例如:

HttpClient httpClient = new HttpClient()
  ..badCertificateCallback =
  ((X509Certificate cert, String host, int port) {
    // tests that cert is self signed, correct subject and correct date(s) 
    return (cert.issuer == cert.subject &&
        cert.subject == 'MySelfSignedCertCN' &&
        cert.endValidity.millisecondsSinceEpoch == 1234567890);
  });

IOClient ioClient = new IOClient(httpClient);
// use ioClient to perform get/post operations from package:http

// don't forget to call ioClient.close() when done
// note, this also closes the underlying HttpClient

推荐阅读