配置本地Tomcat的https
(一)采用keytool生成证书
步骤:
1、 生成证书
keytool -genkey -alias tomcat -keyalg RSA -keystore d:\mykeystore -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass changeit -storepass changeit
参数说明:
1)-genkey 创建一个新的密钥
2)-alias 密钥别名
3)-keyalg 加密算法,这里是RSA
4)-keystore 密钥保存的文件
5)-dname 表示密钥的distinguished names
CN=commonName
OU=oranizationUnit
O = organizationName
L=locatityName
S=stateName
C=country
6)-keypass 私有密钥的密码
7)-storepass 存取密码,用于从存储密钥的文件中(位于-keystore中指定的位置)将信息取出
2、 导出证书(说明:导出证书,由客户端安装)
keytool -export -alias tomcat -keystore d:\mykeystore -file d:\mycerts.cer -storepass changeit
参数说明:
1)-export 导出指定别名的证书到文件
2)-alias 密钥别名
3)-keystore 密钥保存的文件
4)-file 导出到指定的文件中
5)- storepass 密钥保存的文件的存储密码
3、 客户端配置(为客户端的JVM导入密钥(将服务器下发的证书导入到JVM中))
keytool -import -trustcacerts -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file d:\mycerts.cer -storepass changeit
参数说明:
1)-import 将已签名数字证书导入密钥库
2)-trustcacerts
3)-file 数字证书路径
验证是否成功导入到JVM证书库
keytool -list -alias tomcat -keystore "%JAVA_HOME%/jre/lib/security/cacerts " -storepass changeit
4、 其它命令
1、从jvm证书库中删除证书
keytool -delete -alias tomcat –keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit
2、从jvm证书库中导出证书
keytool -export -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit -alias tomcat -file d:\ mycerts.cer
(二)配置Tomcat
1、注销server.xml中的语句:
<!--<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />-->
否则tomcat会出现以下错误:
INFO: Initializing ProtocolHandler ["http-apr-8443"]
Jul 5, 2011 3:20:16 PM org.apache.coyote.AbstractProtocol init
SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-apr-8443"]
java.lang.Exception: Connector attribute SSLCertificateFile must be defined when using SSL with APR
分析见:http://java.dzone.com/articles/ssl-your-tomcat-7
2、放开以下注释
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="D:\mykeystore" keystorePass="changeit" />
|