[HttpClient]SSL双向实例
package com.jerry.httpclient; import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; /**
*
* @author Jerry
* @date 2015年2月12日 下午11:38:33
*/
public class SSLDemo { public static final String KEY_STORE_TYPE_JKS = "jks";
public static final String KEY_STORE_TYPE_P12 = "PKCS12"; public static void main(String[] args) throws Exception{
KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE_P12);//服务器发给浏览器的KeyStore
KeyStore trustStore = KeyStore.getInstance(KEY_STORE_TYPE_JKS);//浏览器发给服务器的KeyStore
FileInputStream input = new FileInputStream(new File("d:\\tomcat.keystore"));
FileInputStream keyInput = new FileInputStream("d:\\mykey.p12");
trustStore.load(input, "123456".toCharArray());
keyStore.load(keyInput, "123456".toCharArray());
/*
* 使用2个keystore创建sslcontext
*/
SSLContext sslContext = SSLContexts.custom().useTLS().
loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "123456".toCharArray())
.build();
//通过sllcontext创建一个socket factory
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslFactory).build();
HttpGet get = new HttpGet("https://localhost:8443/services");
System.out.println("executing request: " + get.getRequestLine());
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("response content lenght: " + entity.getContentLength());
}
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
}
}
[HttpClient]SSL双向实例的更多相关文章
- Netty实现SSL双向验证完整实例
Netty实现SSL双向验证完整实例 博客分类: netty nettyssl自签证书 一.证书准备 要使用ssl双向验证,就必须先要生成服务端和客户端的证书,并相互添加信任,具体流程如下(本人调试 ...
- tomcat配置SSL双向认证
一.SSL简单介绍 SSL(Secure Sockets Layer 安全套接层)就是一种协议(规范),用于保障客户端和服务器端通信的安全,以免通信时传输的信息被窃取或者修改. 怎样保障数据传输安全? ...
- SSL双向认证java实现(转)
本文通过模拟场景,介绍SSL双向认证的java实现 默认的情况下,我认为读者已经对SSL原理有一定的了解,所以文章中对SSL的原理,不做详细的介绍. 如果有这个需要,那么通过GOOGLE,可以搜索到很 ...
- SSL双向认证(高清版)
介绍了SSL双向认证的一些基本问题,以及使用Nginx+PHP基于它搭建https的Webservice. 之前的方式只是实现1:1的模式,昨天同事继续实现了n:1的模式,这里我再整理记录下. 由于n ...
- Nginx、SSL双向认证、PHP、SOAP、Webservice、https
本文是1:1模式,N:1模式请参见新的一篇博客<SSL双向认证(高清版)> ----------------------------------------------------- 我是 ...
- SSL 通信原理及Tomcat SSL 双向配置
SSL 通信原理及Tomcat SSL 双向配置 目录1 参考资料 .................................................................. ...
- apache用户认证,ssl双向认证配置
安装环境: OS:contos 6.4 httpd:httpd-2.2.15-59.el6.centos.i686.rpm openssl:openssl-1.0.1e-57.el6.i686.rpm ...
- ssl双向认证
ssl双向认证 一.背景知识 1.名词解释 ca.key: 根证书的私钥 , ca.crt: 根证书的签名证书 server.key, server.crt client.key, client.cr ...
- 请给你的短信验证码接口加上SSL双向验证
序言 去年年底闲来几天,有位同事专门在网上找一些注册型的app和网站,研究其短信接口是否安全,半天下来找到30来家,一些短信接口由于分析难度原因,没有继续深入,但差不多挖掘到20来个,可以肆意被调用, ...
随机推荐
- 基于新浪sae使用php生成图片发布图文微博
1.生成图片的代码: <?php header ("Content-type: image/png"); mb_internal_encoding("UTF-8&q ...
- sql server R2 下载地址收藏
SQL Server 2008 R2 下载地址 32位: http://care.dlservice.microsoft.com/dl/download/1/e/6/1e626796-588a-495 ...
- 【转】字符串分割(C++)
原文:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/mysplitfuncpp.html 经常碰到字符串分割的问题,这里总结下,也方便我以后使 ...
- Reviewing the Blog Module
Reviewing the Blog Module Throughout the tutorial, we have created a fully functional CRUD module us ...
- 1、C语言中的函数指针
一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...
- linux中shell编程
shell编程 1 echo -e 识别\转义符 \a \b \t \n \x十六进制 \0八进制 等等 #!/bin/bash echo -e "hello world" 执行脚 ...
- Swift数据类型
1.Swift中常用数据类型:首字母大写 Int.Float.Double.Bool.Character.String Array.Dictionary.元组类型(Tuple).可选类型Optiona ...
- PS基础
1.仿制图章工具:[小 ]大 建立一个新的图层,可以进行图片某个部分的复制,完全复制之后,还可以调整大小(ctrl+t), 颜色(ctrl+u打开色相饱和度的菜单)等. 2.修复画笔工具:与仿制 ...
- truncate和 delete的区别:
1.一个表要删除全部数据如何实现:①delete,②truncate EG:删除a表中的所有数据: CREATE TABLE `b` ( `id` int(11) NOT NULL AUTO_INCR ...
- RecyclerView基本用法
1. 在gradle.build的dependencies中添加RecyclerView需要的包 compile 'com.android.support:cardview-v7:23.4.0'com ...