如何为Flutter http创建本地SSL证书以使用

huangapple go评论73阅读模式
英文:

How to create a local SSL certificate for Flutter http to use

问题

我正在使用Flutter的http包,并在本地运行我的REST服务器以进行测试。大约一年前这是有效的,但现在我认为即使是用于测试,我也需要一个SSL证书。

我已经尝试了一些使用openssl的逐步方法,但都失败了。其中一个创建了pem文件,但没有创建证书文件。

是否有关于如何为本地主机创建SSL证书以在Flutter开发中使用的详细步骤说明?

我是否需要与之一起使用特定的IP地址?

或者这是不需要的,有更好的方法来做这个?

英文:

I am using the http pacckage with Flutter and am running my rest server locally for testing purposes. A year or so ago this worked, but now I think I need to have an SSL certificate even for testing.

I've tried a couple of step by steps using openssl, but both failed. One created the pem but not the cert file.

Is there any good step by step instructions on how to create an SSL cert for local host that I can use for Flutter development with http.

And do I need to use a certain ip address with it?

Or is this not needed and is there a better way to do this?

答案1

得分: 0

创建一个类如下:

```dart
import 'dart:io';

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

然后在主函数中(通常在lib文件夹中的main.dart文件中)添加以下代码以实例化你的类:

HttpOverrides.global = new MyHttpOverrides();

此问题已经得到解决,请查看:

获取有关坏证书验证的更多信息。


<details>
<summary>英文:</summary>

create a class like so 

    import &#39;dart:io&#39;;

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

and then in the main function usually in main.dart in the lib folder add this line to instantiate your class 

    HttpOverrides.global = new MyHttpOverrides();

this issue has been dealt with check out :
 * https://stackoverflow.com/questions/64533341/how-to-bypass-ssl-certificate-verification-in-flutter
 * https://stackoverflow.com/questions/59622087/flutter-https-with-self-signed-certificate/66268556#66268556

for more info about the bad certificate validation



</details>



# 答案2
**得分**: 0

```dart
import 'dart:io';

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
  }
}
英文:
import &#39;dart:io&#39;;

class MyHttpOverrides extends HttpOverrides{
@override
// need a null question mark here ✔
HttpClient createHttpClient(SecurityContext? context){
return super.createHttpClient(context)
  ..badCertificateCallback = (X509Certificate cert, String host, int 
port)=&gt; true;
 }
}

huangapple
  • 本文由 发表于 2023年2月8日 17:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定