绕过InAppBrowser Cordova插件中的SSL错误

Bypass SSL Error in InAppBrowser Cordova Plugin

本文关键字:SSL 错误 插件 InAppBrowser Cordova 绕过      更新时间:2023-09-26

我在Cordova项目中添加了一个InAppBrowser插件来访问网站并获取令牌,但当网站在桌面浏览器中正常打开时,从移动浏览器打开时也会出现错误。

此外,在SSL错误的情况下,默认的本地浏览器将要求继续,但Cordova InAppBrowser没有要求这样的选项,而是显示一个错误页面。我使用以下代码打开IAB:

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

你知道如何绕过InAppBrowser中的SSL错误吗?

我将详细介绍相关问题的答案(phonegap inappbrowser https页面未加载)。这只适用于Android,很抱歉仍在iOS上工作。

添加此代码:

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

从插件加载到InAppBrowser.java文件。具体来说,它应该在InAppBrowserClient类下。

希望这能有所帮助!

在插件中插入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

本地https链接在InAppBrowser中默认被阻止(使用第三方无法验证的假SSL证书的链接)。理想情况下,用户应该像默认的桌面浏览器一样,可以选择继续或取消请求

现在,我们必须在InAppBrowser中使用额外的方法来访问伪造的ssl,如位置、缩放、hardwareback