本文将使用keytool和openssl工具来自己生成证书,包含CA,私钥,jks证书管理,配置tomcat的双向认证
1、安装openssl
# tar –zxvf openssl
# cd openssl
# ./config
--prefix=/usr/local/openssl
# make
# make install
2、生成CA证书和私钥
openssl
req -new -x509 -days 18900 -sha1 -newkey rsa:1024 -keyout ca_private.key -out ca.crt
正常情况下CA代表大家都可信任的机构,比如VeriSign机构,但是这里为了测试,自己生成一个CA
3、配置openssl
打开openssl_home/apps/openssl.cnf,找到如下部分
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
#unique_subject
= no # Set to 'no' to
allow creation of
#
several ctificates with same subject.
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/ca.crt # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
# must
be commented out to leave a V1 CRL
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/ca_private.key# The private
key
RANDFILE = $dir/private/.rand # private random number file
修改certificate 和private_key 这两个分别代表CA和私钥,修改为你自己的CA和私钥文件
database ,每生成一个证书,这里会有记录,如果重复生成,可以把之前生成记录删除
new_certs_dir ,这个是证书生成后保存地址
4、创建服务器证书cn要和域名一样
在keystore是keyentry
keytool
-genkey -validity 3650 -keyalg RSA -alias server -keysize 1024 -keystore
tomcat.store
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
5、创建服务证书请求
keytool
-certreq -alias server -sigalg MD5withRSA -file server.csr -keystore tomcat.store
6、用ca证书和私钥签名服务器证书请求(一般由CA中心认证,这里用我自己生成的CA和私钥来签名)
openssl ca -in server.csr -out tomcat_server.crt -notext -config
openssl.cnf
在使用这个命令之前特别主要在openssl中已经配置了ca和private,在生成过程中如果发现某个文件不存在,创建它即可
7、把ca导入到keystore,当作信任证书 在keystore是trustedCertEntry
keytool
-import -v -trustcacerts -alias mas_ca -file ca.crt -keystore
tomcat.store
trustcacerts就是说,在浏览器使用的证书如果要访问该网站,证书必须是由该CA生成或CA的子证书签名生成
8、导入服务器证书到keystore中
keytool
-import -v -file tomcat_server.crt -alias server -keystore tomcat.store
把服务器导入到keystore,此时的keyEntry是有证书链的.如果直接使用keytool的import类型为trustedCertEntry,使用keytool的genkey这时是keyEntry,但是没有证书链,如果要要有证书链可以如上,先签名后用ca认证,再导入ca证书,再import
查看keystore
C:\key>keytool
-v -list -keystore tomcat.store
输入keystore密码:
mas123
Keystore
类型: jks
Keystore
提供者: SUN
您的 keystore 包含 2 输入
别名名称: mas_ca
创建日期: 2011-5-19
输入类型: trustedCertEntry
Owner:
CN=xiongjin, OU=99bill, O=99bill, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,
O=99bill, L=shanghai, ST=shanghai, C=cn
序号: 11f
有效期间: Thu May 19 14:01:09 CST 2011 至: Fri May 18
14:01:09 CST 2012
认证指纹:
MD5:
BD:5F:F2:27:BD:A7:EA:B1:92:D9:D9:06:21:F2:C6:C2
SHA1:
15:86:43:C2:49:B9:CA:60:5F:91:BB:E1:5B:39:91:37:78:F0:9E:66
*******************************************
*******************************************
别名名称: server
创建日期: 2011-5-19
输入类型:KeyEntry
认证链长度: 2
认证 [1]:
Owner:
CN=xiongjin, OU=99bill, O=99bill, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,
O=99bill, L=shanghai, ST=shanghai, C=cn
序号: 11f
有效期间: Thu May 19 14:01:09 CST 2011 至: Fri May 18
14:01:09 CST 2012
认证指纹:
MD5:
BD:5F:F2:27:BD:A7:EA:B1:92:D9:D9:06:21:F2:C6:C2
SHA1:
15:86:43:C2:49:B9:CA:60:5F:91:BB:E1:5B:39:91:37:78:F0:9E:66
认证 [2]:
Owner:
EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill, O=99bill,
L=shanghai, ST=shanghai, C=cn
发照者: EMAILADDRESS=xiongjin1983@gmail.com, CN=localhost, OU=99bill,
O=99bill, L=shanghai, ST=shanghai, C=cn
序号: f71d6ea47619bb8f
有效期间: Thu May 19 09:52:00 CST 2011 至: Mon Jan 10
03:29:36 CST 1927
认证指纹:
MD5:
C2:E0:B1:FD:66:0D:BD:16:D2:2C:C1:87:8A:01:68:16
SHA1:
21:38:9A:28:A0:95:E6:1E:13:42:85:8E:BB:B9:99:4B:8A:0B:0F:06
*******************************************
*******************************************
9、配置tomcat双向认证打开tomcat的 server.xml文件,修改配置
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https"
secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="conf/tomcat.store"
keystorePass="mas123"
truststoreFile="conf/tomcat.store" truststorePass="mas123"/>
使用httpClient访问ssl
//第一个URL对应的keystore为keystore,里面包含客户端证书
//第二个URL对应的keyStore为truststore,基本和服务器的truststore一致
Protocol
authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(new
URL("file:c:/tomcat.store"), "mas123", new URL("file:c:/tomcat.store"),
"mas123"), 8443);
HttpClient
client = new HttpClient();
client.getHostConfiguration().setHost("localhost",
8443, authhttps);
PostMethod
httpget = new PostMethod("/");
int
i = client.executeMethod(httpget);
String
respXml = new String(httpget.getResponseBody(), "UTF8");
System.out.println(respXml);
分享到:
相关推荐
用keytool生成证书,双向SSL认证配置的方法,以Tomcat举例。包含步骤:一、为服务器生成证书;二、为客户端生成证书;三、让服务器信任客户端证书;四、让客户端信任服务器证书。
NULL 博文链接:https://sgq0085.iteye.com/blog/1767923
资源中包含了关于如何使用keytool生成证书,如何在tomcat中配置,并且在java web中如何配置
在tomcat中使用keytool实现双向验证操作视频,按照步骤操作即可,很简单!
一个项目中 客户需求是这样的:指定的电脑可以打开。 在浏览器没安装证书的情况下 客户是无法打开我们的bs系统的。安装证书后可以打开。
NULL 博文链接:https://snowolf.iteye.com/blog/682029
服务端证书包涵服务端私钥和客户端公钥,客户端证书包涵客户端私钥和服务端公钥,两者结合起来可以让服务端和客户端安全的数据交互
很多网站使用https来确保网站的安全性,该文档详细的讲述了,如何使用在tomcat中使用https服务,如何使用jdk自带的keytool工具创建证书,并且实现服务端和客户端的双向认证。
用来测试服务器与客户端握手,可用keytool工具自己生成,其中有个pkcs12格式,用于tomcat双向认证!
keytool 是个密钥和证书管理工具。它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务。它还允许用户储存他们的通信对等者的公钥...
tomcat开发环境配置启用https 使用jdk自带的keytool工具制作证书
压缩包里有客户端源码和服务器端源码,支持TCP的双向认证,也支持WEBSOCKET的双向认证,内附测试 wss的测试例子, 需要生成PKCS12的证书,导入浏览器才可以测试。
使用openssl与jdk的keytool生成根证书,server端证书,浏览器端证书,并配置使工程跑在https上。(带dos脚本)
Keytool与数字证书
openssl+keytool+tomcat自签名证书
通过使用JSSE,可以在Client和Server之间通过TCP/IP协议安全地传输数据。 为了实现消息认证。 Server需要: 1)KeyStore: 其中保存服务端的私钥 2)Trust KeyStore:其中保存客户端的授权证书 Client需要: 1)...
java keytool使用例子 keytool
为了使网站或开发的软件使用https协议访问,使用keytool工具实现生成证书,并在tomcat1.6的配置文件上设置相应的代码实现访问地址形式如:https://localhost/login.jsp。
本问讲述了自己使用OpenSSL keytool等工具自己生成CA证书的详细步骤及使用这些证书的Tomcat服务端设置及客户端IE导入。
java中关于SSL的证书和密钥的形成,关于keytool命令行代码的解释!