1、生成服务器端证书文件
keytool -genkey -alias tomcat -keyalg RSA -keystore /root/cert/tomcat.keystore -validity 36500
参数简要说明:“/root/cert/tomcat.keystore”是证书文件名 ;“-validity 36500”含义是证书有效期,36500表示100年,默认值是90天
Enter keystore password: [password]
Re-enter new password: [password]
What is your first and last name?
[Unknown]: test.xxx.cn
What is the name of your organizational unit?
[Unknown]: xxx.cn
What is the name of your organization?
[Unknown]: xxx
What is the name of your City or Locality?
[Unknown]: shanghai
What is the name of your State or Province?
[Unknown]: shanghai
What is the two-letter country code for this unit?
[Unknown]: zh
Is CN=test.xxx.cn, OU=xxx.cn, O=xxx, L=shanghai, ST=shanghai, C=zh correct?
[no]: yes
Enter key password for <[password]>
(RETURN if same as keystore password): 这里建议不输入密码,直接回车
2、配置TOMCAT服务器
打开$CATALINA_HOME/conf/server.xml,修改如下,
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改参数=>
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"/>
-->
去掉注释且修改参数=>
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/etc/tomcat.keystore" keystorePass="[password]"/>
注释:标识为淡蓝色的两个参数,分别是证书文件的位置和证书密码,在证书文件生成过程中做了设置
<!--
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="8443" />
-->
修改参数=>
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="443" />
(3) 打开$CATALINA_HOME/conf/web.xml,在该文件末尾增加:
2.强制https访问
在tomcatconfweb.xml中的</welcome-file-list>后面加上这样一段:
<login-config>
<!-- Authorization setting for SSL -->
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<!-- Authorization setting for SSL -->
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
3、上述配置完成后,重启TOMCAT后即可以使用SSL。IE地址栏中可以直接输入地址不必输入“http://” 或者 “https://” ;也可以输入 “http:// ” 会跳转成为 “https://” 来登录
4、注意事项:
(1) 生成证书的时间,如果IE客户端所在机器的时间早于证书生效时间,或者晚于有效时间,IE会提示“该安全证书已到期或还未生效”
(2) 如果IE提示“安全证书上的名称无效或者与站点名称不匹配”,则是由生成证书时填写的服务器所在主机的域名“您的名字与姓氏是什么?”/“What is your first and last name?”不正确引起的
--------------------------------------------------------
如果要用于阿里云的SLB,需要再处理一下。
以对应生成的 tomcat.keystore、密码tomcat,别名tomcat,test.xxx.cn证书 为例
1、从JKS的keystore中导出public key (certificate)
keytool -export -alias tomcat -keystore tomcat.keystore -file test.crt
2、转换成PEM格式
openssl x509 -out wx-dev.crt -outform pem -text -in test-pem.crt -inform der
3、导出private key
java ExportPriv tomcat.keystore tomcat tomcat > test-pkcs8.key
4、转成RSA格式的私钥
openssl rsa -inform PEM -in test-pkcs8.key -out test-pem.key
阿里云服务器证书配置,需要 test-pem.crt(公钥)和test-pem.key(私钥),在负载均衡(SLB)里的证书管理界面
5、补充:可以把得到的public key(certificate) 和private key打包在一起,转换成windows平台常用的PKCS12格式
openssl pkcs12 -export -out test.pfx -inkey test-pem.key -in wx-dev-pem.crt
密码 tomcat
如果通过阿里云SLB配置https,还有更简单的办法,参见链接:
http://www.cnblogs.com/lavezhang/p/6277185.html