简体   繁体   English

带有自签名证书的 Flutter https

[英]Flutter https with self signed certificate

I am using flutter to connect with java java server implementation over https.我正在使用flutter通过https连接java java服务器实现。 I first tested it to be working using just http.我首先测试它是否仅使用 http 即可工作。

I then switched to https on the server side and pointed it at my self signed certificate I created using keytool.然后我在服务器端切换到 https 并将其指向我使用 keytool 创建的自签名证书。

Then I tried to connect to it using the http dart package.然后我尝试使用 http dart 包连接到它。 The resulted in the following exception...导致以下异常...

Unhandled Exception: HandshakeException: Handshake error in client (OS Error: E/flutter ( 7370): CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:354))未处理的异常:HandshakeException:客户端中的握手错误(操作系统错误:E/flutter(7370):CERTIFICATE_VERIFY_FAILED:自签名证书(handshake.cc:354))

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...我查看了 APi 参考资料,但不知道如何实现这一点......

My dart code in my flutter app is as follows...我的颤振应用程序中的 dart 代码如下...

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}');
}

While Pascal's answer works, it only applies to the dart:io HttpClient .虽然 Pascal 的答案有效,但它仅适用于dart:io HttpClient To apply the badCertificateCallback to the http package's Client instances, do the following:要将badCertificateCallback应用到http包的Client实例,请执行以下操作:

Create a class that overrides HttpOverrides in the following way:通过以下方式创建一个覆盖HttpOverrides的类:

class DevHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext context) {
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
  }
}

Then in your main, instantiate your class as the global HttpOverride:然后在您的主目录中,将您的类实例化为全局 HttpOverride:

HttpOverrides.global = new DevHttpOverrides();

This should make all Client ignore bad certificates and is therefore onl;y recommended in development.这应该使所有客户端忽略不良证书,因此仅在开发中推荐使用。 Credit goes to this issue: https://github.com/dart-lang/http/issues/458归功于此问题: https : //github.com/dart-lang/http/issues/458

While developing you can use the badCertificateCallback callback of HttpClient and just return true .在开发时,您可以使用 HttpClient 的badCertificateCallback回调并返回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要接受特定的错误证书,您可以从这里尝试使用此代码: 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);

Amazing @Wecherowski, I think more safe way to do this is to check the other details and return true.惊人的@Wecherowski,我认为更安全的方法是检查其他细节并返回true。

Something like:就像是:

HttpClient createHttpClient(SecurityContext? context)
 {
    return super.createHttpClient(context)
       ..badCertificateCallback = (X509Certificate cert, String host, int port)
       {
         if (host.isNotEmpty && host == 'xyz.example.com')
           {  
               return true;  
           }  
           else  
           {  return false;  }  
 };

如果你使用 dio 库https://pub.dev/packages/dio你可以从 http 到 https 发出请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM