瀏覽代碼

fix(Android): add basicAuthCredential prop

iwashita 4 年之前
父節點
當前提交
84f1a5c13f
共有 1 個文件被更改,包括 42 次插入0 次删除
  1. 42
    0
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+ 42
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

@@ -25,6 +25,7 @@ import android.webkit.ConsoleMessage;
25 25
 import android.webkit.CookieManager;
26 26
 import android.webkit.DownloadListener;
27 27
 import android.webkit.GeolocationPermissions;
28
+import android.webkit.HttpAuthHandler;
28 29
 import android.webkit.JavascriptInterface;
29 30
 import android.webkit.RenderProcessGoneDetail;
30 31
 import android.webkit.SslErrorHandler;
@@ -533,6 +534,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
533 534
     view.loadUrl(BLANK_URL);
534 535
   }
535 536
 
537
+  @ReactProp(name = "basicAuthCredential")
538
+  public void setBasicAuthCredential(WebView view, @Nullable ReadableMap credential) {
539
+    @Nullable BasicAuthCredential basicAuthCredential = null;
540
+    if (credential != null) {
541
+      if (credential.hasKey("username") && credential.hasKey("password")) {
542
+        String username = credential.getString("username");
543
+        String password = credential.getString("password");
544
+        basicAuthCredential = new BasicAuthCredential(username, password);
545
+      }
546
+    }
547
+    ((RNCWebView) view).setBasicAuthCredential(basicAuthCredential);
548
+  }
549
+
536 550
   @ReactProp(name = "onContentSizeChange")
537 551
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
538 552
     ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
@@ -777,11 +791,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
777 791
     ReadableArray mUrlPrefixesForDefaultIntent;
778 792
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
779 793
     protected @Nullable String ignoreErrFailedForThisURL = null;
794
+    protected @Nullable BasicAuthCredential basicAuthCredential = null;
780 795
 
781 796
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
782 797
       ignoreErrFailedForThisURL = url;
783 798
     }
784 799
 
800
+    public void setBasicAuthCredential(@Nullable BasicAuthCredential credential) {
801
+      basicAuthCredential = credential;
802
+    }
803
+
785 804
     @Override
786 805
     public void onPageFinished(WebView webView, String url) {
787 806
       super.onPageFinished(webView, url);
@@ -866,6 +885,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
866 885
       return this.shouldOverrideUrlLoading(view, url);
867 886
     }
868 887
 
888
+    @Override
889
+    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
890
+      if (basicAuthCredential != null) {
891
+        handler.proceed(basicAuthCredential.username, basicAuthCredential.password);
892
+        return;
893
+      }
894
+      super.onReceivedHttpAuthRequest(view, handler, host, realm);
895
+    }
896
+
869 897
     @Override
870 898
     public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
871 899
         handler.cancel();
@@ -1215,6 +1243,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1215 1243
       mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1216 1244
     }
1217 1245
 
1246
+    public void setBasicAuthCredential(BasicAuthCredential credential) {
1247
+      mRNCWebViewClient.setBasicAuthCredential(credential);
1248
+    }
1249
+
1218 1250
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1219 1251
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1220 1252
     }
@@ -1470,3 +1502,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1470 1502
     }
1471 1503
   }
1472 1504
 }
1505
+
1506
+class BasicAuthCredential {
1507
+  String username;
1508
+  String password;
1509
+
1510
+  BasicAuthCredential(String username, String password) {
1511
+    this.username = username;
1512
+    this.password = password;
1513
+  }
1514
+}