在Cordova for Android上忽略无效的SSL证书

时间:2016-09-12 09:12:17

标签: java android cordova ssl

我有一个使用无效SSL证书的cordova应用程序。

在3.3.6中,我可以通过扩展org.apache.cordova.CordovaWebViewClient并覆盖public void onReceivedSslError方法来覆盖SSL证书,即

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.CordovaInterface;

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

public class MyWebViewClient extends CordovaWebViewClient {

    public MyWebViewClient(CordovaInterface cordova, CordovaWebView view) {
        super(cordova, view);
    }

    @Override
    public void onReceivedSslError
    (WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

}

然后从起始Java文件我可以使用

MyWebViewClient myWebViewClient = new MyWebViewClient(this, this.appView);
this.appView.setWebViewClient(myWebViewClient);

升级到5.2.2后,由于WebView已更改为Crosswalk,因此无法使用此类内容。我根据Ignoring invalid SSL certificates on Cordova for Android and iOS的指南尝试的是将上面的代码更改为:

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.engine.SystemWebViewClient;
import org.apache.cordova.engine.SystemWebViewEngine;
import org.apache.cordova.CordovaInterface;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

public class myWebViewClient extends SystemWebViewClient {

    public myWebViewClient(SystemWebViewEngine systemWebViewEngine) {

        super(systemWebViewEngine);
    }
    @Override
    public void onReceivedSslError
    (WebView view, SslErrorHandler handler, SslError error) {


            handler.proceed();
        }

    }

在代码中调用:

CordovaActivity ca = (CordovaActivity) this.cordovaInterface.getActivity();
SystemWebView view = (SystemWebView) appView.getView();
myWebViewClient cWebClient = new myWebViewClient((SystemWebViewEngine) appView.getEngine());
appView.clearCache();
view.setWebViewClient(cWebClient);

但是我收到以下异常

Unknown error during webview in activity
java.lang.ClassCastException: org.crosswalk.engine.XWalkCordovaView cannot be cast to org.apache.cordova.engine.SystemWebView
    at myProject.InitApp.onCreate(initApp.java:182)
    at android.app.Activity.performCreate(Activity.java:6289)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

对此有何帮助?似乎Crosswalk不允许这样的压倒一切。有没有办法覆盖它?

0 个答案:

没有答案