Flutter中自签名证书上的SSL握手错误

Arm*_*Kh. 11 ssl android flutter

我正在尝试使用自签名证书连接服务器,但我
收到错误:E/flutter(3781):HandshakeException:客户端中的握手错误(操作系统错误:
E/flutter(3781):CERTIFICATE_VERIFY_FAILED:主机名不匹配(ssl_cert.c :345))
代码,我设置证书:

String path = '/storage/sdcard0/server.crt';
SecurityContext context = new SecurityContext();
context.setTrustedCertificates(path, password: 'hello');
_client = new HttpClient(context: context);
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

如果我没有设置SecurityContext,我会收到SSL握手错误.

Arm*_*Kh. 10

我使用了HttpClient.badCertificateCallback
这是一个接受任何证书的代码:

_client = new HttpClient();
_client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
Run Code Online (Sandbox Code Playgroud)

  • 它有效,但它没有回答原来的问题。接受所有证书是一个糟糕的选择。我遇到了同样的问题,并且不明白为什么设置“setTrustedCertificates”不起作用。 (5认同)

Col*_*son 5

您可以从https://letsencrypt.org/免费获得有效的 SSL 证书