1、生成keystore文件
keytool -v -genkey -alias tomcat -keyalg RSA -keystore /home/test/my.keystore
在生成keystore的过程中,要输入一些站点信息和密码,并要求再次核对密码
2、编辑tomcat/conf/server.xml
找到对应的connector,取消注释,并且写入keystore文件路径和密码
1 2 3 4 5 |
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="/home/test/my.keystore" keystorePass="123456"/> |
3、强制HTTP转HTTPS 对工程的web.xml进行修改,加入:
1 2 3 4 5 6 7 8 9 |
<security-constraint> <web-resource-collection> <web-resource-name>OPENSSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> |
4、禁用不安全的http方法 在tomcat/conf/web.xml最后加上一个节点
1 2 3 4 5 6 7 8 9 10 11 |
<security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint></auth-constraint> </security-constraint> |
注:经测试第3和第4可合并为:
<security-constraint>
<!-- Authorization setting for SSL -->
<web-resource-collection>
<web-resource-name>OPENSSL</web-resource-name>
<url-pattern>/*</url-pattern>
<!--禁用HTTP的不安全方法-->
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint></auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
---------------------------------------------------------------------------------------
https如何进行加密传输
客户端是没有证书的,也就没有公钥和私钥。
SSL握手阶段,服务器把证书传输给客户端,同时也就传输了公钥(公钥是证书的一部分)。
由客户端来对这个证书进行有效性认可,再由这个客户端来生成对称密钥。
对称密钥用服务器证书中的公钥加密后,传回给服务器。只有服务器才能解密这个信息,也就只有服务器才知道你的对称密钥。
只要这个SSL连接没有关闭,后续的所有数据,无论是客户端发出的还是服务器发出的,均会使用这个对称密钥加密。
对称加密算法中,依赖的是密钥的保密性,只要密钥没有被泄露,对称加密的结果被截获也没有什么意义。而密钥是用公钥加密的,只能由服务器解开。
===================================
所以在SSL连接没有建立的时候,服务器给客户端发的可以被截获的数据仅有证书,没有其他的数据。其他的数据都要等SSL连接建立之后加密传输。
所以https的证书加密还是安全的,在没有私钥的情况下不存在泄露的可能。