带有自签名证书的Flutter https [英] Flutter https with self signed certificate

查看:215
本文介绍了带有自签名证书的Flutter https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter通过https连接Java Java服务器实现.我首先测试了它是否可以仅使用http.

I am using flutter to connect with java java server implementation over https. I first tested it to be working using just http.

然后我在服务器端切换到https,并将其指向我使用keytool创建的自签名证书.

I then switched to https on the server side and pointed it at my self signed certificate I created using keytool.

然后,我尝试使用http dart软件包连接到它.结果导致以下异常...

Then I tried to connect to it using the http dart package. The resulted in the following exception...

未处理的异常:HandshakeException:客户端(操作系统)中的握手错误错误:E/Flutter(7370):CERTIFICATE_VERIFY_FAILED:自签名证书(handshake.cc:354))

Unhandled Exception: HandshakeException: Handshake error in client (OS Error: E/flutter ( 7370): CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:354))

我假设我需要设置客户端信任我的服务器自签名证书.我查看了APi参考,但不知道如何实现此目的...

I am assuming I need to set my client to trust my servers self signed certificate. I have looked at the APi reference and could not figure out how to get this to happen...

在我的flutter应用程序中,我的飞镖代码如下...

My dart code in my flutter app is as follows...

void testMessage() {
    var url = 'https://192.168.100.105:8443';
    var response = await http.post(url, body: "{\"message_name\": \"TestMessage\", \"contents\": { \"field1\":\"blah\", \"field2\":\"blah\" }}");
    print('Response status: ${response.statusCode}');
    print('Response body: ${response.body}');
}

推荐答案

在开发过程中,您可以使用HttpClient的 badCertificateCallback 回调,而只需返回 true .这将接受所有错误的证书.

While developing you can use the badCertificateCallback callback of HttpClient and just return true. This will accept all bad certificates.

  HttpClient client = HttpClient()
    ..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);

要接受特定的不良证书,您可以在此处尝试以下代码:

To accept a specific bad certificate you may experiment with this code from here: https://github.com/dart-lang/http/issues/14#issuecomment-311184690

import 'dart:io';
import 'package:http/http.dart' as http;

bool _certificateCheck(X509Certificate cert, String host, int port) =>
    host == 'local.domain.ext'; // <- change

HttpClient client = new HttpClient()
    ..badCertificateCallback = (_certificateCheck);

这篇关于带有自签名证书的Flutter https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆