1、利用JDK自带的keytools生成一个p12类型的证书

keytool -genkey -storetype PKCS12 -alias tomcat -keyalg RSA -keysize 2048 -keystore E:\tomcat.p12 -storepass 123456 -dname "CN=zhi,OU=zhi,O=zhi,L=wuhan,ST=hubei,C=CN"

  参考:https://www.cnblogs.com/zhi-leaf/p/10418222.html

2、修改conf/server.xml文件,打开如下配置

  certificateKeystoreFile:证书位置;certificateKeystorePassword:密码

  tomcat启动日志信息:

3、现在我们就可以使用https进行访问了。

4、其他

  当然,我们也可以用OpenSSL生成证书,参考资料:https://www.cnblogs.com/zhi-leaf/p/11987394.html

openssl genrsa -out tomcat.key 2048 // 生成私钥
openssl req -new -x509 -days 3650 -key tomcat.key -out tomcat.crt -subj "/C=CN/ST=hubei/L=wuhan/O=zhi/OU=zhi/CN=tomcat" // 生成自签名证书

  配置server.xml

<Connector port="6001" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="150" SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/tomcat.key"
                     certificateFile="conf/tomcat.crt"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

  certificateKeyFile:私钥;certificateFile:证书;certificateChainFile:证书链,可选。

  小技巧,我们可以使用下面命令将证书格式进行转换:

openssl x509 -in test.crt -out test.pem // 证书转换,crt格式转成pem格式

 

posted on 2019-12-05 15:51  玄同太子  阅读(6381)  评论(0编辑  收藏  举报