设为首页 收藏本站
查看: 371|回复: 0

[经验分享] java tomcat 搭建SSL双向认证以及httpclient 代码

[复制链接]

尚未签到

发表于 2017-2-5 13:32:46 | 显示全部楼层 |阅读模式
最近一直在尝试搭建apache+tomcat的单向认证ssl环境。网上看了很多令我眼花缭乱的资料。先开始用mod_jk的方式始终不能成功。后来还是用mod_proxy方式来实现。但这种方式貌似性能不是太好,这里如果有朋友知道确实可行的资料。可以告诉小弟一声。
然后又开始研究apache+tomcat的双向认证环境。没找到apache+tomcat的解决方案。倒是找到只是基于tomcat的双向认证方案。经我认证是可行的。。在此要谢谢原创作者。
Java Tomcat SSL 服务端/客户端双向认证(一)
Java Tomcat SSL 服务端/客户端双向认证のApache HttpClient(二)
另外附一下httpclient发送http请求、发送https单向认证请求、发送https双向请求代码
我用的httpClient版本是4.2.2,低版本的可能不支持。建议大家大家去官网下载一个完整的发行包

package com.cntaiping.clm.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
private static Log log=LogFactory.getLog(HttpClientUtils.class);
private static final String KEY_STORE_TYPE_JKS = "jks";
private static final String KEY_STORE_TYPE_P12 = "PKCS12";
private static final String SCHEME_HTTPS = "https";
private static final int HTTPS_PORT = 8443;
private static final String HTTPS_URL = "https://127.0.0.1:8443/TestDemo/sslServlet";
private static final String KEY_STORE_CLIENT_PATH = "D:/key/client.p12";
private static final String KEY_STORE_TRUST_PATH = "D:/key/client.truststore";
private static final String KEY_STORE_PASSWORD = "aaaaaa";
private static final String KEY_STORE_TRUST_PASSWORD = "aaaaaa";
/**
* 使用httpClient 发出http请求
* 均为POST请求
* @param url
* @param xmlStr
* @return
*/
public static String  httpClient(String url,String xmlStr) {
String returnStr="";
HttpClient client = new DefaultHttpClient();
HttpPost post=new HttpPost(url);
post.setHeader("content-type", "text/xml; charset=UTF-8");
StringEntity strEntity;
try {
strEntity = new StringEntity(xmlStr, "UTF-8");
post.setEntity(strEntity);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if(null!=entity){
BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
StringBuffer buff=new StringBuffer();
while((returnStr=reader.readLine())!=null){
buff.append(returnStr);
}
returnStr=buff.toString();
//returnStr=EntityUtils.toString(entity);
log.info("returnStr:"+returnStr);
post.abort();
if(response != null) {
Header headers[] = response.getAllHeaders();
int i = 0;
while (i < headers.length) {
log.debug(headers.getName() + ":  "+ headers.getValue());
i++;
}
}
}
} catch (UnsupportedEncodingException e) {
log.info("程序出错:"+e.getMessage());
} catch (ClientProtocolException e) {
log.info("程序出错:"+e.getMessage());
} catch (IOException e) {
log.info("程序出错:"+e.getMessage());
}finally{
client.getConnectionManager().shutdown();
}
return returnStr;
}
/**
* 发送https请求
* 为单向请求
* @param url
* @param xmlStr
* @return
*/
public static String httpsClient(String url,String xmlStr) {
long responseLength = 0;                         //响应长度  
String responseContent = "";                     //响应内容  
HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例  
X509TrustManager xtm = new X509TrustManager(){   //创建TrustManager  
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
public X509Certificate[] getAcceptedIssuers() {
return null;   //return new java.security.cert.X509Certificate[0];  
}
};  
try {  
//TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext  
SSLContext ctx = SSLContext.getInstance("TLS");  
//使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用  
ctx.init(null, new TrustManager[]{xtm}, null);  
//创建SSLSocketFactory  
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);  
//通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上  
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));  
HttpPost httpPost = new HttpPost(url);                        //创建HttpPost  
//            List<NameValuePair> formParams = new ArrayList<NameValuePair>(); //构建POST请求的表单参数  
//            for(Map.Entry<String,String> entry : params.entrySet()){  
//                formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
//            }  
httpPost.setEntity(new StringEntity(xmlStr, "UTF-8"));  
HttpResponse response = httpClient.execute(httpPost); //执行POST请求  
HttpEntity entity = response.getEntity();             //获取响应实体  
if (null != entity) {  
responseLength = entity.getContentLength();  
responseContent = EntityUtils.toString(entity, "UTF-8");  
EntityUtils.consume(entity); //Consume response content  
}  
System.out.println("请求地址: " + httpPost.getURI());  
System.out.println("响应状态: " + response.getStatusLine());  
System.out.println("响应长度: " + responseLength);  
System.out.println("响应内容: " + responseContent);  
} catch (KeyManagementException e) {  
e.printStackTrace();  
} catch (NoSuchAlgorithmException e) {  
e.printStackTrace();  
} catch (UnsupportedEncodingException e) {  
e.printStackTrace();  
} catch (ClientProtocolException e) {  
e.printStackTrace();  
} catch (ParseException e) {  
e.printStackTrace();  
} catch (IOException e) {  
e.printStackTrace();  
} finally {  
httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源  
}
return responseContent;
}
/**
* 发送https请求
* 为双向请求
* @param url
* @param xmlStr
* @return
*/
private static void ssl() throws Exception {
HttpClient httpClient = new DefaultHttpClient();
try {
KeyStore keyStore  = KeyStore.getInstance(KEY_STORE_TYPE_P12);
KeyStore trustStore  = KeyStore.getInstance(KEY_STORE_TYPE_JKS);
InputStream ksIn = new FileInputStream(KEY_STORE_CLIENT_PATH);
InputStream tsIn = new FileInputStream(new File(KEY_STORE_TRUST_PATH));
try {
keyStore.load(ksIn, KEY_STORE_PASSWORD.toCharArray());
trustStore.load(tsIn, KEY_STORE_TRUST_PASSWORD.toCharArray());
} finally {
try { ksIn.close(); } catch (Exception ignore) {}
try { tsIn.close(); } catch (Exception ignore) {}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, KEY_STORE_PASSWORD, trustStore);
Scheme sch = new Scheme(SCHEME_HTTPS, HTTPS_PORT, socketFactory);
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet(HTTPS_URL);
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
String text;
while ((text = bufferedReader.readLine()) != null) {
System.out.println(text);
}
bufferedReader.close();
}
EntityUtils.consume(entity);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-337852-1-1.html 上篇帖子: [Tomcat源码系列]结构解析 2)生命期控制结构 下篇帖子: (转)SSL 通信原理及Tomcat SSL 双向配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表