4

我试图在需要 ssl 身份验证的 web 视图中查看 https url。我遇到了与这篇文章类似的问题: 如何在 WebView 中处理基本身份验证,我收到 401 未经授权的错误。

我不希望用户输入用户名或密码,因为我正在使用证书进行身份验证。我有两种方式获得客户端证书,作为使用密钥库的 X509Certificate 和作为 bks 密钥库。

任何人都可以帮助我,我应该如何告诉 webview 在加载 url 时使用这个证书。

4

1 回答 1

1

代码来自https://github.com/potaka001/WebViewBasicAuthTest

当然,您对 onReceivedHttpAuthRequest 方法感兴趣。

package com.webviewbasicauthtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.CookieManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyWebViewClient extends WebViewClient {
  private String loginCookie;
  private Context mContext;
  private WebView mWebView;
  public MyWebViewClient(Context context, WebView webview) {

  super();
  mContext = context;
  mWebView = webview;
}

@Override
public void onPageStarted( WebView view, String url, Bitmap favicon ) {
}

@Override
public void onPageFinished( WebView view, String url ) {
  CookieManager cookieManager = CookieManager.getInstance();
  cookieManager.setCookie(url, loginCookie);
}

@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
  Toast.makeText(view.getContext(), "ページ読み込みエラー",   Toast.LENGTH_LONG).show();
}

@Override
public void onLoadResource( WebView view, String url ){
  CookieManager cookieManager = CookieManager.getInstance();
  loginCookie = cookieManager.getCookie(url);
}

@Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
  return false;
}

@Override
public void onReceivedSslError( WebView view, SslErrorHandler handler, SslError error ) {
  handler.proceed();
}

@Override
public void onReceivedHttpAuthRequest( WebView view, final HttpAuthHandler handler, final String host, final String realm ){

  String userName = null;
  String userPass = null;

  if (handler.useHttpAuthUsernamePassword() && view != null) {
    String[] haup = view.getHttpAuthUsernamePassword(host, realm);

  if (haup != null && haup.length == 2) {
    userName = haup[0];
    userPass = haup[1];
 }
}

if (userName != null && userPass != null) {
    handler.proceed(userName, userPass);
}
else {
    showHttpAuthDialog(handler, host, realm, null, null, null);
}
}

private void showHttpAuthDialog( final HttpAuthHandler handler, final String host, final String realm, final String title, final String name, final String password ) {
    LinearLayout llayout = new LinearLayout((Activity)mContext);
    final TextView textview1 = new TextView((Activity)mContext);
    final EditText edittext1 = new EditText((Activity)mContext);
    final TextView textview2 = new TextView((Activity)mContext);
    final EditText edittext2 = new EditText((Activity)mContext);
    llayout.setOrientation(LinearLayout.VERTICAL);
    textview1.setText("username:");
    textview2.setText("password:");
    llayout.addView(textview1);
    llayout.addView(edittext1);
    llayout.addView(textview2);
    llayout.addView(edittext2);

    final Builder mHttpAuthDialog = new AlertDialog.Builder((Activity)mContext);

    mHttpAuthDialog.setTitle("Basic Authentication")
    .setView(llayout)
    .setCancelable(false)
    .setPositiveButton("OK", new    DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {
  EditText etUserName = edittext1;
  String userName = etUserName.getText().toString();
  EditText etUserPass = edittext2;
  String userPass = etUserPass.getText().toString();
  mWebView.setHttpAuthUsernamePassword(host, realm, name, password);
  handler.proceed(userName, userPass);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
  handler.cancel();
}
})
.create().show();
}
}
于 2015-02-21T19:24:59.043 回答