SSL error prevents website login in flutter

2020-08-01 00:44发布

Hay,

as a replacement for Xamarin I tried to create a app with the same functions in flutter. But the webview plugin/package doens't work with the self signed ssl certificate. In Xamarin there was a way to handle the ssl errors and still get the website loaded.

Current code:

  @override
  void initState() {
    super.initState();
    final flutterWebviewPlugin = new FlutterWebviewPlugin();
    flutterWebviewPlugin.onHttpError.skip(1000);
    flutterWebviewPlugin.launch("https url to website", hidden: true,
        allowFileURLs: true,
        appCacheEnabled: true,
        withJavascript: true,
        withLocalStorage: true,
        withZoom: true);

flutterWebviewPlugin.onHttpError.skip(1000);
flutterWebviewPlugin.onStateChanged((state) async)
{
  flutterWebviewPlugin.evalJavascript(
      "document.getElementById(\"name\").innerText = \"username\"");
  flutterWebviewPlugin.evalJavascript(
      "document.getElementById(\"password\").innerText = \"pw\"");
  flutterWebviewPlugin.evalJavascript(
      "document.getElementById(\"loginbutton\").invokeMember(\"click\")");
  var test = flutterWebviewPlugin.evalJavascript(
      "document.getElementById(\"errorbox\").innerText");
};
flutterWebviewPlugin.dispose();
flutterWebviewPlugin.close();

Error:

Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Is there a better way then writing an own package or an custom webview in java and swift?

标签: dart flutter
1条回答
乱世女痞
2楼-- · 2020-08-01 01:37

On Android you could modify the plugin. Clone a copy to your local machine and add an additional override to flutter_webview_plugin/android/src/main/java/com/flutter_webview_plugin/BrowserClient.java

Override public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) with an implementation that simply does handler.proceed()

Let's say your project is in /projects/myproject and you cloned the plugin into /projects/flutter_webview_plugin. Make the change in /projects/flutter_webview_plugin/android/src/.../BrowserClient.java. Then update the pubspec.yaml of your project with (replaces the existing entry for flutter_webview_plugin):

dependencies:
  flutter_webview_plugin:
    path: ../flutter_webview_plugin
查看更多
登录 后发表回答