前景:项目做安全策略需要配置远程https模式登录,先前采用nginx配置方式,踩了无数坑最后放弃了T_T,后来采用修改tomcat配置方法完成,防止时间长了自己忘记,简单的写了这个笔记。
1、在Linux下生成证书
我这里将证书放在了当前目录下,如果要指定路径,则配置/path/tomcat.teystore即可(path表示你的具体路径,如:/home/test/tomcat.teystore)
在centos上执行如下命令: keytool -genkey -v -alias tomcat -keyalg RSA -keystore tomcat.keystore -validity 36500 Enter keystore password: (这里输入密码,后面会用到) Re-enter new password: What is your first and last name? [Unknown]: localhost(这里写域名,不是自己的名字) What is the name of your organizational unit? [Unknown]: xxxxx (组织) What is the name of your organization? [Unknown]: xxxxx (组织) What is the name of your City or Locality? [Unknown]: chengdu What is the name of your State or Province? [Unknown]: sichuan What is the two-letter country code for this unit? [Unknown]: CN Is CN=localhost, OU=xxxxx, O=xxxxx, L=chengdu, ST=sichuan, C=CN correct? [no]: y2、修改tomcat配置文件
找到tomcat下的conf/server.xml文件中的如下部分,去掉注释,并添加证书路径和密码(步骤1中输入的密码)
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" keystoreFile="/home/test/tomcat.teystore" keystorePass="xxx@123" clientAuth="false" sslProtocol="TLS" />
3、重启tomcat即可通过https的方式访问页面
注:此方法在tomcat8.0和tomcat9.0上验证成功,但是8.5失败(由于时间关系,我没有继续修改)。当时项目使用的是8.5.56版本的tomcat,按照配置死活不成功,后来替换为8.0.52版本,成功。并且后续在9.0.13上也可成功。
