Service worker with self signed certificate on Android WebView

Viewed 882

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 Answers

This will cause the app to always accept untrusted certs.

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

Better solution is use your certicate

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