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

本文介绍了如何将ssl.cer和ssl.key转换为ssl.jks文件,然后将其部署到Tomcat服务器以实现HTTPS。通过在线工具合并证书和密钥,设置证书别名和JKS密码。接着,在Tomcat的server.xml和web.xml中进行相关配置,包括SSLHostConfig和强制跳转到HTTPS。最后,通过8443端口访问时会实现强制HTTPS连接。参考了两篇优快云博客文章进行详细步骤说明。

现有文件: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.youkuaiyun.com/hgghxl/article/details/44939351 
    https://blog.youkuaiyun.com/wangyuanjiet/article/details/77062169
     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值