Kaynağa Gözat

fix(android): Add SSL error handling for Android WebView (#1450 by @thephpjedi)

Based on PR #668. Fixes #259.
Alesandro Ortiz 4 yıl önce
ebeveyn
işleme
1bd5961255
No account linked to committer's email address

+ 46
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Dosyayı Görüntüle

@@ -10,6 +10,7 @@ import android.content.pm.PackageManager;
10 10
 import android.graphics.Bitmap;
11 11
 import android.graphics.Color;
12 12
 import android.Manifest;
13
+import android.net.http.SslError;
13 14
 import android.net.Uri;
14 15
 import android.os.Build;
15 16
 import android.os.Environment;
@@ -26,6 +27,7 @@ import android.webkit.CookieManager;
26 27
 import android.webkit.DownloadListener;
27 28
 import android.webkit.GeolocationPermissions;
28 29
 import android.webkit.JavascriptInterface;
30
+import android.webkit.SslErrorHandler;
29 31
 import android.webkit.PermissionRequest;
30 32
 import android.webkit.URLUtil;
31 33
 import android.webkit.ValueCallback;
@@ -797,6 +799,50 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
797 799
       return this.shouldOverrideUrlLoading(view, url);
798 800
     }
799 801
 
802
+    @Override
803
+    public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
804
+        handler.cancel();
805
+
806
+        int code = error.getPrimaryError();
807
+        String failingUrl = error.getUrl();
808
+        String description = "";
809
+        String descriptionPrefix = "SSL error: ";
810
+
811
+        // https://developer.android.com/reference/android/net/http/SslError.html
812
+        switch (code) {
813
+          case SslError.SSL_DATE_INVALID:
814
+            description = "The date of the certificate is invalid";
815
+            break;
816
+          case SslError.SSL_EXPIRED:
817
+            description = "The certificate has expired";
818
+            break;
819
+          case SslError.SSL_IDMISMATCH:
820
+            description = "Hostname mismatch";
821
+            break;
822
+          case SslError.SSL_INVALID:
823
+            description = "A generic error occurred";
824
+            break;
825
+          case SslError.SSL_NOTYETVALID:
826
+            description = "The certificate is not yet valid";
827
+            break;
828
+          case SslError.SSL_UNTRUSTED:
829
+            description = "The certificate authority is not trusted";
830
+            break;
831
+          default: 
832
+            description = "Unknown SSL Error";
833
+            break;
834
+        }
835
+        
836
+        description = descriptionPrefix + description;
837
+
838
+        this.onReceivedError(
839
+          webView,
840
+          code,
841
+          description,
842
+          failingUrl
843
+        );
844
+    }
845
+    
800 846
     @Override
801 847
     public void onReceivedError(
802 848
       WebView webView,