首页 > 代码库 > OpenSSL/Tomcat HTTPS 搭建
OpenSSL/Tomcat HTTPS 搭建
总结一下 OpenSSL和Tomcat https的搭建
第一部分:首先是看看 OpenSSL的搞法:
创建证书的步骤:
(1)生成私钥
(2)生成待签名证书
(3)生成x509证书, 用CA私钥进行签名
(4)导成浏览器支持的p12格式证书
一:生成CA证书
CA
1. 创建私钥 :
openssl genrsa -out ca/ca-key.pem 1024
2.创建证书请求 :
openssl req -new -out ca/ca-req.csr -key ca/ca-key.pem
3.自签署证书 :
openssl x509 -req -in ca/ca-req.csr -out ca/ca-cert.pem -signkey ca/ca-key.pem -days 1000
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in ca/ca-cert.pem -inkey ca/ca-key.pem -out ca/ca.p12
密码:xxxxxxx
三.生成server证书
1.创建私钥 :
openssl genrsa -out server/server-key.pem 1024
2.创建证书请求 :
openssl req -new -out server/server-req.csr -key server/server-key.pem
3.自签署证书 :
openssl x509 -req -in server/server-req.csr -out server/server-cert.pem
-signkey server/server-key.pem -CA ca/ca-cert.pem -CAkey ca/ca-key.pem
-CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in server/server-cert.pem -inkey server/server-key.pem -out server/server.p12
密码:xxxxx
四.生成client证书
1.创建私钥 :
openssl genrsa -out client/client-key.pem 1024
2.创建证书请求 :
openssl req -new -out client/client-req.csr -key client/client-key.pem
openssl x509 -req -in client/client-req.csr -out client/client-cert.pem
-signkey client/client-key.pem -CA ca/ca-cert.pem -CAkey ca/ca-key.pem
-CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :
openssl pkcs12 -export -clcerts -in client/client-cert.pem -inkey client/client-key.pem -out client/client.p12
五.根据ca证书生成jks文件 (Java keystore)
keytool -keystore truststore.jks -keypass 222222 -storepass 222222 -alias ca -import -trustcacerts -file ca/ca-cert.pem
第二部分 .配置tomcat ssl
1. conf/server.xml。
tomcat6中多了SSLEnabled="true"属性。keystorefile, truststorefile设置为你正确的相关路径
xml 代码
tomcat 5.5的配置:
<Connector port="8443" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="server.p12" keystorePass="changeit" keystoreType="PKCS12"
truststoreFile="truststore.jks" truststorePass="222222" truststoreType="JKS" />
tomcat6.0的配置:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="server.p12" keystorePass="changeit" keystoreType="PKCS12"
truststoreFile="truststore.jks" truststorePass="222222" truststoreType="JKS"/>
七、测试(linux下)
openssl s_client -connect localhost:8443 -cert
/home/ssl/client/client-cert.pem -key /home/ssl/client/client-key.pem
-tls1 -CAfile /home/ssl/ca/ca-cert.pem -state -showcerts
GET /index.jsp HTTP/1.0
八、导入证书
服务端导入server.P12 和ca.p12证书
客户端导入将ca.p12,client.p12证书
IE中(打开IE->;Internet选项->内容->证书)
ca.p12导入至受信任的根证书颁发机构,client.p12导入至个人
Firefox中(工具-选项-高级-加密-查看证书-您的证书)
将ca.p12和client.p12均导入这里
注意:ca,server,client的证书的common name(ca=ca,server=localhost,client=dong)一定不能重复,否则ssl不成功
九、tomcat应用程序使用浏览器证书认证
在server/webapps/manager/WEB-INF/web.xml中,将BASIC认证改为证书认证
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Tomcat Manager Application</realm-name>
</login-config>
在conf/tomcat-users.xml中填入下列内容
<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="admin"/>
<role rolename="user"/>
<user username="EMAILADDRESS=dong@dong.com, CN=dong, OU=tb, O=tb, L=bj, ST=bj, C=cn" password="null" roles="admin,user,manager"/>
</tomcat-users>
访问http://localhost:8443即可验证ssl是否成功
访问http://localhost:8443/manager/html可验证应用程序利用client证书验证是否成功
附件:
批量创建证书的格式:
#!/bin/bash
# using sample
# sh genClient.sh 20160728_Client001 "CHANGSHA SHINING POWER ELECTRONICS CO.,LTD" DS2015-F0105-00104 james.taoA1@murata.com
/usr/bin/expect <<EOF
set time 30
spawn openssl req -new -key shdcweb1client.pem -out client/$1.csr -sha256
expect {
"Country Name" {send "CN\r";exp_continue }
"State or Province Name" {send "ShangHai\r";exp_continue }
"Locality Name" {send "ShangHai\r";exp_continue }
"Organization Name" {send "Murata\r";exp_continue }
"Organizational Unit" {send "MCI\r";exp_continue }
"Common Name" {send "$2\r";exp_continue }
"Email Address" {send "$4\r";exp_continue }
"A challenge password" {send "murata\r";exp_continue }
"An optional company name" {send "MCI\r";exp_continue }
}
spawn openssl ca -policy policy_anything -days 365 -cert shdcweb1ca.crt -keyfile shdcweb1cakey.pem -in client/$1.csr -out client/$1.crt
expect {
"Enter pass phrase" {send "CH61is@2016\r";exp_continue }
"Sign the certificate" {send "y\r";exp_continue }
"1 out of 1 certificate requests certified" {send "y\r";exp_continue }
}
spawn openssl pkcs12 -export -clcerts -in client/$1.crt -inkey shdcweb1client.pem -out client/$1.p12
expect {
"Enter Export Password" {send "$3\r";exp_continue }
"Verifying - Enter Export Password" {send "$3\r" }
}
EOF
~
本文出自 “莫长空” 博客,请务必保留此出处http://silversnow.blog.51cto.com/285506/1924465
OpenSSL/Tomcat HTTPS 搭建