webview.setWebViewClient(new WebViewClient() { public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { if (realm.equals(expectedRealm) && host.equals(ipaddress)) { handler.proceed(username, password); } else { handler.cancel(); } } });
@Override public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { Context context = view.getContext(); AlertDialog.Builder builder = new AlertDialog.Builder(context); LayoutInflater layoutInflater = LayoutInflater.from(context); View dialogView = layoutInflater.inflate(R.layout.auth_dialog, new LinearLayout(context)); EditText username = dialogView.findViewById(R.id.username); EditText password = dialogView.findViewById(R.id.password); TextView auth_detail = dialogView.findViewById(R.id.auth_detail); String text = context.getString(R.string.auth_dialog_detail, view.getUrl()); auth_detail.setText(text); builder.setView(dialogView) .setTitle(R.string.auth_dialog_title) .setPositiveButton(R.string.auth_dialog_login, (dialog, whichButton) -> handler.proceed( username.getText().toString(), password.getText().toString())) .setNegativeButton(android.R.string.cancel, (dialog, whichButton) -> handler.cancel()) .setOnDismissListener(dialog -> handler.cancel()) .show(); }
@Override public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host, final String realm) { Log.i(TAG, "realm " + realm); CredentialsHelper.getInstance(getContext()).handleAuthRequest(host, realm, handler, () -> { final AlertDialog alert = new AlertDialog.Builder(getContext()) .setCancelable(false) .setTitle(R.string.credentials_required) .setMessage(getContext().getString(R.string.host_realm, host, realm)) .setView(R.layout.dialog_credentials) .setPositiveButton(R.string.okay, (dialog12, id) -> { EditText userT = ((AlertDialog) dialog12).findViewById(R.id.username); EditText passT = ((AlertDialog) dialog12).findViewById(R.id.password); CheckBox storeCB = ((AlertDialog) dialog12).findViewById(R.id.checkBox); if (storeCB.isChecked()) { CredentialsHelper.getInstance(getContext()).registerCredentials(host, realm, userT.getText().toString(), passT.getText().toString()); } handler.proceed(userT.getText().toString(), passT.getText().toString()); }).setNegativeButton(R.string.cancel, (dialog1, which) -> handler.cancel()) .create(); if (getContext() != null && !((Activity) getContext()).isFinishing()) { alert.show(); } }); } });
handler.cancel();