android-webview – 带证书的Android 4.0 Webview SSL身份验证

android-webview – 带证书的Android 4.0 Webview SSL身份验证,第1张

概述我试图在webview中查看需要ssl身份验证的https网址. 我对这篇文章有类似的问题: How to handle Basic Authentication in WebView 我得到401未经授权的错误. 我不希望用户输入用户名或密码,因为我正在使用证书进行身份验证. 我有两种方式获得客户端证书,作为使用密钥库的X509Certificate和作为bks密钥库. 任何人都可以帮助我如何在 我试图在webvIEw中查看需要ssl身份验证的https网址.
我对这篇文章有类似的问题:
How to handle Basic Authentication in WebView
我得到401未经授权的错误.

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

任何人都可以帮助我如何在加载网址时告诉webvIEw使用此证书.

解决方法 代码是 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;}@OverrIDepublic voID onPageStarted( WebVIEw vIEw,String url,Bitmap favicon ) {}@OverrIDepublic voID onPageFinished( WebVIEw vIEw,String url ) {  cookieManager cookieManager = cookieManager.getInstance();  cookieManager.setcookie(url,logincookie);}@OverrIDepublic voID onReceivedError( WebVIEw vIEw,int errorCode,String description,String failingUrl ) {  Toast.makeText(vIEw.getContext(),"ページ読み込みエラー",Toast.LENGTH_LONG).show();}@OverrIDepublic voID onLoadResource( WebVIEw vIEw,String url ){  cookieManager cookieManager = cookieManager.getInstance();  logincookie = cookieManager.getcookie(url);}@OverrIDepublic boolean shouldOverrIDeUrlLoading( WebVIEw vIEw,String url ) {  return false;}@OverrIDepublic voID onReceivedSslError( WebVIEw vIEw,SslErrorHandler handler,SslError error ) {  handler.proceed();}@OverrIDepublic 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);}}private voID showhttpAuthDialog( final httpAuthHandler handler,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,name,password);  handler.proceed(username,userPass);}}).setNegativebutton("Cancel",new DialogInterface.OnClickListener() {  public voID onClick(DialogInterface dialog,int whichbutton) {  handler.cancel();}}).create().show();}}
总结

以上是内存溢出为你收集整理的android-webview – 带证书的Android 4.0 Webview SSL身份验证全部内容,希望文章能够帮你解决android-webview – 带证书的Android 4.0 Webview SSL身份验证所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1124617.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存