Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL error #298

Open
FightingDEV opened this issue Jan 15, 2019 · 21 comments
Open

SSL error #298

FightingDEV opened this issue Jan 15, 2019 · 21 comments

Comments

@FightingDEV
Copy link

Hay,

i'm having a xamp server on a machine with a self signed certificate.
I need to login to the website but because of the ssl the website is white.

`
@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();

}
`

Is there a way to catch up the ssl error "Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found." and ignore it?

@AlexV525
Copy link

AlexV525 commented Mar 26, 2019

Do you still stuck on it? I have a solution.

@FightingDEV
Copy link
Author

Thanks for the answer.
I fixed it already by adding the OnReceivedSSlError in the Android and IOS part of the webview.

Do you have got an other solution?

@AlexV525
Copy link

Same as you.😁 Bad certificate sucks.

@jessicaxxxxxxx
Copy link

hi, my friends, can i share your solution, where could i add the OnReceivedSSlError and do you still use this webview plugin? i have the same question, but i just start to learn flutter app.
hope your reply.

@AlexV525
Copy link

AlexV525 commented Apr 4, 2019

@jessicaxxxxxxx Hello mate :) Solution goes here:
Locate to flutter\.pub-cache\hosted\pub.flutter-io.cn\flutter_webview_plugin-0.3.3\android\src\main\java\com\flutter_webview_plugin\WebviewManager.java
Add below content to the code.

import android.net.http.SslError;
import android.webkit.SslErrorHandler;

image

Then line 85:
webViewClient = new BrowserClient();
Changed to:

webViewClient = new BrowserClient() {
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
};

image

@jessicaxxxxxxx
Copy link

@AlexVincent525 thanks for your quick reply. but it seems that you integrated flutter in a native android project, and i used this plugin in native flutter project, so my files are all .dart files, i can not find the above path and .java file. so do you have the same experences?

@AlexV525
Copy link

AlexV525 commented Apr 4, 2019

@AlexVincent525 thanks for your quick reply. but it seems that you integrated flutter in a native android project, and i used this plugin in native flutter project, so my files are all .dart files, i can not find the above path and .java file. so do you have the same experences?

Nope. My project is flutter native too. This file was not in your flutter project, it's a dart package which located in flutter sdk.
About how to locate this file, in Android Studio for example, Ctrl+LeftClickMouse on new FlutterWebviewPlugin() in your code, the path will shown on top. Then you can find this file as what i said before.

@jessicaxxxxxxx
Copy link

@AlexVincent525 thanks for your quick reply. but it seems that you integrated flutter in a native android project, and i used this plugin in native flutter project, so my files are all .dart files, i can not find the above path and .java file. so do you have the same experences?

Nope. My project is flutter native too. This file was not in your flutter project, it's a dart package which located in flutter sdk.
About how to locate this file, in Android Studio for example, Ctrl+LeftClickMouse on new FlutterWebviewPlugin() in your code, the path will shown on top. Then you can find this file as what i said before.

hi, AlexVincent525 I got it, it was very useful for android, and i have tried to solve the problem for ios in the same way, but i failed, so could you do me a favor again? please

@AlexV525
Copy link

AlexV525 commented Apr 8, 2019

@jessicaxxxxxxx Hi, plz paste your error message here, cause iOS might not facing this problem.

@E2-Mohamed
Copy link

E2-Mohamed commented Jul 24, 2019

@AlexVincent525: i'm using Kotlin in flutter not java, i'm not able to locate the file WebviewManager.java, but i've reached here

webview

and the by cmd+leftclick on FlutterWebViewPlugin() i went here

image

Now what should i do ?

@AlexV525
Copy link

@mohamedyaseen Hi, you haven't reached the android path. Right click on webview_scaffold.dart and select Show in Explorer, then go to ../../android/src/main/java/com/flutter_webview_plugin, you'll see it. It's not related whether you are using kotlin or java.

@E2-Mohamed
Copy link

image
Got it, Here it is :) Thanks for your help.

@E2-Mohamed
Copy link

@jessicaxxxxxxx i had the same problem in iOS, i fixed it by adding the following code in FlutterWebviewPlugin.m

image

`- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
CFDataRef exceptions = SecTrustCopyExceptions(serverTrust);
SecTrustSetExceptions(serverTrust, exceptions);
CFRelease(exceptions);

completionHandler(NSURLSessionAuthChallengeUseCredential,
                  [NSURLCredential credentialForTrust:serverTrust]);

}`

@avirias
Copy link

avirias commented Aug 9, 2019

@jessicaxxxxxxx Hello mate :) Solution goes here:
Locate to flutter\.pub-cache\hosted\pub.flutter-io.cn\flutter_webview_plugin-0.3.3\android\src\main\java\com\flutter_webview_plugin\WebviewManager.java
Add below content to the code.

import android.net.http.SslError;
import android.webkit.SslErrorHandler;

image

Then line 85:
webViewClient = new BrowserClient();
Changed to:

webViewClient = new BrowserClient() {
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
};

image

It helped me. thanks man

@fkranenburg
Copy link

This solution works, but I don't like to change the source code this way. Is there another option?

@charafau
Copy link
Collaborator

@fkranenburg what think would be to add this code to plugin as specify variable (it would be also good to add handler for those errors)

other thing is - does it work on ios ?

@jameshp
Copy link
Contributor

jameshp commented Oct 30, 2019

is anybody working on that? otherwise I could give it a try to add an option to ignore ssl certificate errors, maybe with some warning debug messages that this should not be a prodcution config.

on your second question @charafau:
From the above comments I can see that @mohamedyaseen has made a workaround for iOS.

@pichillilorenzo
Copy link

Hi guys!

You can use my plugin flutter_inappwebview. It has a lot of events, including events to manage SSL errors and SSL client certificate requests:

  • onReceivedServerTrustAuthRequest: Event fired when the WebView need to perform server trust authentication (certificate validation). This is implemented using the onReceivedSslError event on Android.
  • onReceivedClientCertRequest: Notify the host application to handle an SSL client certificate request.

So, in your case, you need to use only the onReceivedServerTrustAuthRequest event and return simply ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController webView;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://myUrl",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                        inAppWebViewOptions: InAppWebViewOptions(
                          debuggingEnabled: true,
                        ),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {

                    },
                    onReceivedServerTrustAuthRequest: (InAppWebViewController controller, ServerTrustChallenge challenge) async {
                      return ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);
                    },
                  ),
                ),
              ),
            ]))
    );
  }
}

where "https://myUrl" is your url.

@diogoarm
Copy link

diogoarm commented Dec 6, 2019

@marcqtan
Copy link

Hi guys!

You can use my plugin flutter_inappwebview. It has a lot of events, including events to manage SSL errors and SSL client certificate requests:

  • onReceivedServerTrustAuthRequest: Event fired when the WebView need to perform server trust authentication (certificate validation). This is implemented using the onReceivedSslError event on Android.
  • onReceivedClientCertRequest: Notify the host application to handle an SSL client certificate request.

So, in your case, you need to use only the onReceivedServerTrustAuthRequest event and return simply ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController webView;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://myUrl",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                        inAppWebViewOptions: InAppWebViewOptions(
                          debuggingEnabled: true,
                        ),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {

                    },
                    onReceivedServerTrustAuthRequest: (InAppWebViewController controller, ServerTrustChallenge challenge) async {
                      return ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);
                    },
                  ),
                ),
              ),
            ]))
    );
  }
}

where "https://myUrl" is your url.

Hello, it's working fine in Android. However, it's only white screen in IOS. Can you please share how to handle it in IOS using the flutter_inappwebview?

@pichillilorenzo

@naveenkumardot25
Copy link

My Solution
I got a problem, InAppWebview not providing page URL that I want in onLoadStart iOS and in Android it's working fine. So I decided to use iosOnDidReceiveServerRedirectForProvisionalNavigation.

iosOnDidReceiveServerRedirectForProvisionalNavigation:  (InAppWebViewController controller) async {
    String url = await controller.getUrl();
    Uri uri = Uri.dataFromString(url);
    if (uri.queryParameters.containsKey('status') &&
         uri.queryParameters.containsKey('order') &&
         uri.queryParameters.containsKey('txnid')) {
                Transaction transaction = new Transaction.fromJson(uri.queryParameters);
                context.bloc<CartCubit>().setData(transaction: transaction);
                Navigator.pop(context);
    }
},

Now it's working in iOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests