Service worker with self signed certificate on Android WebView

时间:2019-01-07 12:56:14

标签: android certificate android-webview service-worker

Is it possible to use service worker with self-signed certificates?

I know that it is possible on the desktop using the flag --unsafely-treat-insecure-origin-as-secure= at the start (https://stackoverflow.com/a/43484456).

But how can I achieve this on Android WebView?

I've already created a custom WebViewClient to skip all the SSL errors:

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

but this does not seem to help here.

1 个答案:

答案 0 :(得分:-1)

这将导致该应用始终接受不受信任的证书。

public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
        if ( SslError.SSL_UNTRUSTED == error.getPrimaryError() ){
            handler.proceed();
        } else {
            super.onReceivedSslError(view, handler, error);
        }
    }

更好的解决方案是使用您的certicate

String sslCertificate = error.getCertificate().toString();
    String mySslCertificate = new SslCertificate(cert).toString(); //cert is stored inside /raw 
    if ( sslCertificate.equals(mySslCertificate) )
        handler.proceed();