How to clear Http Basic Auth username and password from cache - android webview

Viewed 841

I am using android webview to display a web page that requires basic authentication. I have the code that takes in username and password and allows successful login to the web page. But I want to clear the username and password from cache when the user visits the webview again. Looks like once you enter username and password for basic auth, it keeps that in cache and logs you in automatically.

I have tried

    WebViewDatabase webViewDB = WebViewDatabase.getInstance(getActivity());
    webViewDB.clearHttpAuthUsernamePassword();

I have also tried webview.clearFormData, webview.clearCache

But it doesn't seem to work. It logged me in automatically without the prompt to enter username and password.

Here is my full code for webview fragment.

public class WebViewFragment extends Fragment {
private static final String LOG_TAG = WebViewFragment.class.getName();
private ProgressBar progressBar;
private View footerV;
private String urlToLoad;
private int footerVisibility;
private int failureCount = 0;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.web_fragment, container, false);
    try {
        init(rootView);
    } catch (Exception e) {
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(getActivity()));
    }
    return rootView;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (footerV != null) footerV.setVisibility(footerVisibility);
}

private void init(View rootView) {
    Bundle bundle = getArguments();
    ConstraintLayout navBarCL = (ConstraintLayout) rootView.findViewById(R.id.navBar);
    progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
    if (bundle.getBoolean(BundleConstants.WEB_HIDE_HEADER)) {
        navBarCL.setVisibility(View.GONE);
    } else {
        navBarCL.setVisibility(View.VISIBLE);

        ImageView backButtonIV = (ImageView) rootView.findViewById(R.id.backButtonImage);
        backButtonIV.setOnClickListener(view -> navigateBack());

        String title = bundle.getString(BundleConstants.WEB_HEADER_TEXT);
        TextView headerTextTV = (TextView) rootView.findViewById(R.id.headerText);
        headerTextTV.setText(title);
    }

    WebView webView = (WebView) rootView.findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.setWebViewClient(getClient());
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onCloseWindow(WebView window) {
            super.onCloseWindow(window);
            navigateBack();
        }
    });

    footerV = getActivity().findViewById(R.id.footer);
    footerVisibility = footerV.getVisibility();
    if (bundle.getBoolean(BundleConstants.WEB_SHOW_FOOTER)) {
        footerV.setVisibility(View.VISIBLE);
    } else {
        footerV.setVisibility(View.GONE);
    }

    urlToLoad = bundle.getString(BundleConstants.WEB_URL);
    Log.d(LOG_TAG, "Opening url :" + urlToLoad);
    webView.loadUrl(urlToLoad);
}

@NonNull
private WebViewClient getClient() {

    WebViewDatabase webViewDB = WebViewDatabase.getInstance(getActivity());
    webViewDB.clearHttpAuthUsernamePassword();
    return new WebViewClient() {
        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            ++failureCount;
            showProgressBar();
            LoginDialog loginDialog = new LoginDialog(getContext(), getFragment(), handler, failureCount);
            loginDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            hideProgressBar();
        }

    };

}

public void hideProgressBar() {
    if (progressBar != null) progressBar.setVisibility(View.GONE);
}

public void showProgressBar() {
    if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
}

private WebViewFragment getFragment() {
    return this;
}

public String getUrlToLoad() {
    return urlToLoad;
}

protected void navigateBack() {
    footerV.setVisibility(footerVisibility);
    getFragmentManager().popBackStack();
}

}

Please provide some suggestions.

1 Answers
Related