https双向认证demo
阅读此文首先需要具备数据加解密,公钥,私钥,签名,证书等基础知识。
通信服务端:使用tomcat
通信客户端:使用apache httpclient 4.5.1
1. 服务端操作
.
keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:\servesert\tomcat.keystore -validity
终端问答的参数:
CN=helloworld.com, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown
keypassword storepassword 这步生成服务端的私钥和证书。 keystore是一个秘钥和证书的容器,里面可以装多个秘钥和证书。alias是一个keystore里证书或秘钥的唯一的识别id。CN=helloworld.com是服务端的域名,做测试时,客户端机器需要配置helloworld.com的hosts。 keytool -list -v -keystore D:\servesert\tomcat.keystore
查看该keystore里的证书和私钥。
keytool -keystore D:\servesert\tomcat.keystore -export -alias tomcat -file D:\servesert\tomcat.cer
将tomcat.keystore这个秘钥库里alias为tomcat的公钥导出为证书,这个证书将添加到客户端的受信任秘钥库中。
2.客户端操作
keytool -genkey -v -alias me -keyalg RSA -keystore D:\cert\me.keystore -validity
CN=hello, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown
keypassword storepassword
生成客户端秘钥,CN名字可以随便填 keytool -import -v -alias tomcat -file D:\servesert\tomcat.cer -keystore D:\cert\trust.keystore
将服务端导出的证书添加到客户端受信任的秘钥库中。 keytool -list -v -keystore D:\cert\me.keystore
keytool -list -v -keystore D:\cert\trust.keystore keytool -keystore D:\cert\me.keystore -export -alias me -file D:\cert\me.cer 将客户端私钥库中的客户端证书导出,之后会添加到服务端的受信任证书库。
3.服务端操作
keytool -import -v -alias me -file D:\cert\me.cer -keystore D:\servecert\trust.keystore
将客户端的证书导入服务器的受信任证书库
4.tomcat server.xml配置
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS" keystoreFile="D:\\servesert\\tomcat.keystore"
keystorePass="123456" truststoreFile="D:\\servesert\\trust.keystore"
truststorePass="123456" />
5.java客户端代码
package com.xy; 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.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.ssl.SSLContexts; import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException; /**
* Created by xiaxia on 2015/11/4.
*/
public class Test {
public static void main(String[] args) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnrecoverableKeyException {
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new File("D:\\cert\\trust.keystore"), "123456".toCharArray(),
new TrustSelfSignedStrategy())
.loadKeyMaterial(new File("D:\\cert\\me.keystore"), "123456".toCharArray(), "123456".toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build(); try { HttpGet httpget = new HttpGet("https://helloworld.com:8443/hello/testget.json"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(entity)); EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
} // HttpPost httpPost = new HttpPost("http://targethost/login");
// List <NameValuePair> nvps = new ArrayList <NameValuePair>();
// nvps.add(new BasicNameValuePair("username", "vip"));
// nvps.add(new BasicNameValuePair("password", "secret"));
// httpPost.setEntity(new UrlEncodedFormEntity(nvps));
// CloseableHttpResponse response2 = httpclient.execute(httpPost);
//
// try {
// System.out.println(response2.getStatusLine());
// HttpEntity entity2 = response2.getEntity();
// // do something useful with the response body
// // and ensure it is fully consumed
// EntityUtils.consume(entity2);
// } finally {
// response2.close();
// }
}
说明:如果自己的私钥库中有超过一个可选证书,系统会默认选择第一个。若受信任证书库中有超过一个证书,会遍历证书直到找到第一个匹配的。
参见http://stackoverflow.com/questions/6370745/can-we-load-multiple-certificates-keys-in-a-key-store
https双向认证demo的更多相关文章
- Android Https双向认证 + GRPC
keywords:android https 双向认证android GRPC https 双向认证 ManagedChannel channel = OkHttpChannelBuilder.for ...
- HTTPS 双向认证构建移动设备安全体系
HTTPS 双向认证构建移动设备安全体系 对于一些高安全性要求的企业内项目,我们有时希望能够对客户端进行验证.这个时候我们可以使用Https的双向认证机制来实现这个功能. 单向认证:保证server是 ...
- Tomcat 配置 HTTPS双向认证
Tomcat 配置 HTTPS 双向认证指引说明: � 本文档仅提供 Linux 操作系统下的指引 � 在阅读本指引前请您在 Linux 部署 JDK 和 Tomcatserver为了 Tomcat ...
- httpd设置HTTPS双向认证
去年用tomcat.jboss配置过HTTPS双向认证,那时候主要用的是JDK自带的keytool工具.这次是用httpd + openssl,区别比较大 在网上搜索了很多文章,发现全面介绍的不多,或 ...
- Https双向认证Android客户端配置
Https .cer证书转换为BKS证书 公式https://blog.csdn.net/zww986736788/article/details/81708967 keytool -importce ...
- 双向认证 HTTPS双向认证
[微信支付]微信小程序支付开发者文档 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3 HTTPS双向认证使用说明 ...
- https双向认证訪问管理后台,採用USBKEY进行系统訪问的身份鉴别,KEY的证书长度大于128位,使用USBKEY登录
近期项目需求,须要实现用USBKEY识别用户登录,採用https双向认证訪问管理后台管理界面,期间碰到过一些小问题,写出来给大家參考下. 1:前期准备工作 USBKEY 硬件:我买的是飞天诚信 epa ...
- nodejs之https双向认证
说在前面 之前我们总结了https的相关知识,如果不懂可以看我另一篇文章:白话理解https 有关证书生成可以参考:自签证书生成 正题 今天使用nodejs来实现https双向认证 话不多说,直接进入 ...
- SpringBoot服务间使用自签名证书实现https双向认证
SpringBoot服务间使用自签名证书实现https双向认证 以服务server-one和server-two之间使用RestTemplate以https调用为例 一.生成密钥 需要生成server ...
随机推荐
- Notepad++配置Python运行环境
转自:http://www.cnblogs.com/zhcncn/p/3969419.html Notepad++配置Python开发环境 1. 安装Python 1 下载 我选择了32位的2.7 ...
- [置顶] Android AlarmManager实现不间断轮询服务
在消息的获取上是选择轮询还是推送得根据实际的业务需要来技术选型,例如对消息实时性比较高的需求,比如微博新通知或新闻等那就最好是用推送了.但如果只是一般的消息检测比如更新检查,可能是半个小时或一个小时一 ...
- python基于http协议编程:httplib,urllib和urllib2<转>
httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. httplib.HTTPConnecti ...
- PL/pgSQL学习笔记之六
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3.1. 声明函数参数 传递给函数的参数被用 $1.$2等 ...
- jeecg团队招新人(5人)
jeecg团队招新人(5人) http://www.jeecg.org/forum.php? mod=viewthread&tid=2046&page=1&extra=#pid ...
- jQuery + jQuery Mobile 实现省市二级下拉列表页面
一.需求: 提供省.市下拉列表,当用户选择省一级下拉列表项后,市下拉列表的各个选项自动变为该省对应的城市列表. 二.效果: 三.实现: 1.省市json数据,来自: http://www.cnblog ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A. Slime Combining 水题
A. Slime Combining 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2768 Description Your frien ...
- java 计算一个方法的返回执行时间
开始时间 long startTime = System.currentTimeMillis(); 程序业务逻辑代码() 结束时间 long endTime = System.currentTi ...
- [Express] Level 5: Route file
Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and ...
- C++中临时对象的学习笔记
http://www.cppblog.com/besterChen/category/9573.html 所属分类: C/C++/STL/boost 在函数调用的时候,无论是参数为对象还是返回一个对 ...