栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用HttpClient 4.0.1与x509证书进行相互身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用HttpClient 4.0.1与x509证书进行相互身份验证

这是一些代码,助您一臂之力。该

KeyStore
是包含客户端证书的对象。如果服务器使用的是自签名证书,或者包含的cacerts文件中的JVM认可的不是CA所签名的证书,则需要使用
TrustStore
。否则,要使用默认的cacerts文件,请传递
null
SSLSockeFactory
truststore参数。

import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpParams;...final HttpParams httpParams = new BasicHttpParams();// load the keystore containing the client certificate - keystore type is probably jks or pkcs12final KeyStore keystore = KeyStore.getInstance("pkcs12");InputStream keystoreInput = null;// TODO get the keystore as an InputStream from somewherekeystore.load(keystoreInput, "keystorepassword".toCharArray());// load the trustore, leave it null to rely on cacerts distributed with the JVM - truststore type is probably jks or pkcs12KeyStore truststore = KeyStore.getInstance("pkcs12");InputStream truststoreInput = null;// TODO get the trustore as an InputStream from somewheretruststore.load(truststoreInput, "truststorepassword".toCharArray());final SchemeRegistry schemeRegistry = new SchemeRegistry();schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keystore, keystorePassword, truststore), 443));final DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438187.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号