使用cer和key生成jks,并部署到tomcat上

现有文件:ssl.cer和ssl.key

  1. 打开在线JKS合成工具,将ssl.cer和ssl.key输入2个文本框,然后合成一个ssl.jks文件。(https://www.myssl.cn/tools/merge-jks-cert.html)
  2. 输入证书别名和jks密码
  3. 将ssl.jks 放入tomcat/config文件夹里,config目录下的server.xml
  4. 找到SSLHostConfig,解开注释,做好如下配置(以 http端口 8081 https端口8443 为例子
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
                   maxThreads="150" SSLEnabled="true">
    	<SSLHostConfig>
                <Certificate certificateKeystoreFile="conf/ssl.jks" 
    			certificateKeystorePassword="123456"
                             type="RSA" clientAuth="false" sslProtocol="TLS" />
            </SSLHostConfig>
        </Connector>

     

  5. 配置强制跳转地址:

    <Connector port="8081" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" maxPostSize="-1" maxHttpHeaderSize ="102400" RIEncoding="UTF-8" />

     

  6. server.xml配置完成,接下来配置config下的web.xml

  7. 在<web-app>标签中添加以下代码(加到最后就可以,即</web-app>前)

        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
        </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> 

     

  8. 现在整个已经配置完成,重启tomcat 。按照我上面通过配置的8081和8443端口访问 都会强制跳转到8443连接中。

  9. 例如ca证书的认证名字叫localhost, 那么 http://localhost:8081 和https:/localhost:8443 都会强制到https连接。将8443端口改为443,就会变成https:/localhost 访问项目。

  10. 参考文章:https://blog.csdn.net/hgghxl/article/details/44939351 
    https://blog.csdn.net/wangyuanjiet/article/details/77062169
     

 


版权声明:本文为wenyanxing原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。