关于https通信流程请参考本人另外一篇博文https握手流程总结
自签名证书:非官方CA颁发的证书,此处做测试使用
jdk中keytool工具使用生成证书
1、为服务器生成证书
管理员身份运行cmd,进入jdk安装路径的bin下
使用keytool命令生成证书:
keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/tomcat.keystore -storepass 123456
参数说明:
keytool-genkey
-alias tomcat(别名)
-keypass 123456(别名密码)
-keyalg RSA(算法)
-keysize 1024(密钥长度)
-validity 365(有效期,天单位)
-keystore D:/tomcat.keystore(指定生成证书的位置和证书名称)
-storepass 123456(获取keystore信息的密码)
执行完毕后可在D盘根目录下看到名为tomcat.keystore
的文件。
2、为客户端生成证书
此处为浏览器生成证书,方便服务器验证它。
keytool -genkey -alias client -keypass 123456 -keyalg RSA -storetype PKCS12 -keypass 123456 -storepass 123456 -keystore D:/client.p12
参数说明:
-storetype PKCS12(证书格式)
3、服务器添加客户端证书信任
1)将.p12格式的证书导出为CER文件,方便导入证书库中:
keytool -export -alias client -keystore D:/client.p12 -storetype PKCS12 -keypass 123456 -file D:/client.cer
2)将client.cer导入到服务器的证书库中,添加为一个信任证书:
keytool -import -v -file D:/client.cer -keystore D:/tomcat.keystore -storepass 123456
查看服务器证书库:
keytool -list -v -keystore D:/tomcat.keystore
4、客户端添加服务器证书信任
双向SSL认证过程中,不仅服务端要对客户端进行认证,客户端也要对服务器端进行认证。即把服务器证书添加到浏览器的“受信任的根证书颁发机构”
1)将tomcat.keystore导出为cer文件
keytool -keystore D:/tomcat.keystore -export -alias tomcat -file D:/server.cer
2)双击产生的server.cer进行证书安装
3)将证书填入到“受信任的根证书颁发机构”
打开浏览器 - 工具 - internet选项-内容- 证书-把中间证书颁发机构里的yzy(该名称即时你前面生成证书时填写的名字与姓氏)证书导出来-再把导出来的证书导入受信任的根颁发机构
tomcat配置
通过上述操作,我们已经产生了自签名证书并在浏览器和服务器互相添加了证书信任。下面,就通过配置tomcat容器,实现服务协议的转换。
1、server.xml配置
取消注释
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="250" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="D:/tomcat.keystore"
keystorePass = "123456" truststoreFile="D:/tomcat.keystore"
truststorePass="123456"/>属性说明:
clientAuth:设置是否双向验证,默认为false,设置为true代表双向验证
keystoreFile:服务器证书文件路径
keystorePass:服务器证书密码
truststoreFile:用来验证客户端证书的根证书,此例中就是服务器证书
truststorePass:根证书密码文件端口更改
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" acceptCount="100" maxThreads="250"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
注意:
① 设置clientAuth属性为True时,需要手动导入客户端证书才能访问。
② 要访问https请求 需要访问8443端口,访问http请求则访问Tomcat默认端口(你自己设置的端口,默认8080)即可。
2、强制 https 访问
在 tomcat /conf/web.xml 中加上:
<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>
如此一来你使用HTTPS 端口为8443 进行访问的时候 就是经过SSL信息加密,不怕被截获了。
通话的双方,必须是都拥有证书的端,才能进行会话,换句话说,就是只有安装了证书的客户端,才能与服务器通信。