绕过InAppBrowser Cordova插件中的SSL错误

时间:2014-10-24 12:30:37

标签: javascript cordova inappbrowser

我已将一个InAppBrowser插件添加到Cordova项目中以访问网站并获取令牌,但是当桌面浏览器中的网站正常打开时,从移动浏览器打开时也会出错。

此外,默认本机浏览器会在SSL出现错误时继续请求继续,但Cordova InAppBrowser不会要求此类选项,而是显示错误页面。我使用以下代码打开IAB:

var iab = window.open('http://www.example.com', '_blank', 'location=yes');

如何绕过InAppBrowser中的SSL错误?

2 个答案:

答案 0 :(得分:6)

我将扩展相关问题(phonegap inappbrowser https pages not loading)的答案。这仅适用于Android,抱歉仍在使用iOS。

添加此代码:

    public void onReceivedSslError(WebView view,
                SslErrorHandler handler, SslError error) {
        Log.e("Error", "Received SSL error"+ error.toString());
        handler.proceed();
    }

从插件中获取InAppBrower.java文件。具体来说,它应该在InAppBrowserClient类下。

希望这有帮助!

答案 1 :(得分:2)

将InAppBrowser.java中的正确代码插入到您的插件中 位于平台\ android \ src \ org \ apache \ cordova \ inappbrowser \ InAppBrowser.java

来自以下java代码的过滤代码:

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

@SuppressLint("SetJavaScriptEnabled")
public class InAppBrowser extends CordovaPlugin {


    private boolean ignoreSSLError = false;


    private HashMap<String, Boolean> parseFeature(String optString) {
        if (optString.equals(NULL)) {
            return null;
        } else {
            HashMap<String, Boolean> map = new HashMap<String, Boolean>();
            StringTokenizer features = new StringTokenizer(optString, ",");
            StringTokenizer option;
            while(features.hasMoreElements()) {
                option = new StringTokenizer(features.nextToken(), "=");
                if (option.hasMoreElements()) {
                    String key = option.nextToken();
                    if(key.equalsIgnoreCase(IGNORE_SSL_ERROR)) {
                        Boolean value = option.nextToken().equals("no") ? Boolean.FALSE : Boolean.TRUE;
                        map.put(key, value);
                    }
                    else {
                        Boolean value = option.nextToken().equals("no") ? Boolean.FALSE : Boolean.TRUE;
                        map.put(key, value);
                    }

                }
            }
            return map;
        }
    }



    public String showWebPage(final String url, HashMap<String, Boolean> features) {
        // Determine if we should hide the location bar.
        showLocationBar = true;
        showZoomControls = true;
        openWindowHidden = false;
        ignoreSSLError = false;
        if (features != null) {
            Boolean show = features.get(LOCATION);
            if (show != null) {
                showLocationBar = show.booleanValue();
            }
            Boolean SSLError = features.get(IGNORE_SSL_ERROR);
            if(SSLError != null){
                ignoreSSLError = SSLError.booleanValue();
            }
            Boolean zoom = features.get(ZOOM);
            if (zoom != null) {
                showZoomControls = zoom.booleanValue();
            }
            Boolean hidden = features.get(HIDDEN);
            if (hidden != null) {
                openWindowHidden = hidden.booleanValue();
            }
            Boolean hardwareBack = features.get(HARDWARE_BACK_BUTTON);
            if (hardwareBack != null) {
                hadwareBackButton = hardwareBack.booleanValue();
            }
            Boolean cache = features.get(CLEAR_ALL_CACHE);
            if (cache != null) {
                clearAllCache = cache.booleanValue();
            } else {
                cache = features.get(CLEAR_SESSION_CACHE);
                if (cache != null) {
                    clearSessionCache = cache.booleanValue();
                }
            }
        }


            @SuppressLint("NewApi")
            public void run() {

                ((InAppBrowserClient) client).setSSLErrorFlag(ignoreSSLError);

            }
        };
        this.cordova.getActivity().runOnUiThread(runnable);
        return "";
    }



    public class InAppBrowserClient extends WebViewClient {
        EditText edittext;
        CordovaWebView webView;
        boolean ignoreSSLError = false;


        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
            if(this.ignoreSSLError) {
                handler.proceed();
                return;
            }
            else{
                super.onReceivedSslError(view, handler, error);
            }
        }
        public void setSSLErrorFlag(boolean flag) {
            this.ignoreSSLError = flag;
        }

    }
}

THEN ADD THIS LINE IN JAVASCRIPT 

    var options = {
      location: 'yes',
      //clearcache: 'no',
      toolbar: 'yes',
    //clearsessioncache:'no',
          zoom:'no',
          ignoresslerror:'yes'
    };


    $scope.init = function () {
 $ionicPlatform.ready(function() {
 $cordovaInAppBrowser.open('https://192.168.1.80', '_blank', options)
      .then(function(event) {
      })
      .catch(function(event) {
      });
     });

AFTER DONE THIS COMPILE AND EXECUTE THAT'S IT 

FULL VERSION CODE

默认情况下,InAppBrowser会阻止本地https链接(使用假SSL证书的链接无法由第三方验证)。理想情况下,应为用户提供继续或取消请求的选项,如默认桌面浏览器所做的那样。

现在,我们必须使用其他方法来访问InAppBrowser中的假ssl,例如location,zoom,hardwareback