如何在Tomcat中配置SSL证书

时间:2019-05-19 01:25:45  来源:igfitidea点击:

我们假设我们已经在系统中安装了Tomcat服务器。
本文既可以用于Linux主机,也可以用于Windows主机,唯一需要更改的是密钥库的目录路径。

步骤1 -创建密钥存储库

Java密钥存储库(JKS)是安全证书的存储库。
是用于创建和管理密钥存储的命令行实用程序。
该命令对JDK和JRE都可用。
我们只需要确保JDK或JRE配置了PATH环境变量。

keytool -genkey -alias svr1.theitroad.com -keyalg RSA -keystore /etc/pki/keystore

(输出示例)

Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  Hyman Kumar
What is the name of your organizational unit?
  [Unknown]:  Web
What is the name of your organization?
  [Unknown]:  theitroad Inc.
What is the name of your City or Locality?
  [Unknown]:  Delhi
What is the name of your State or Province?
  [Unknown]:  Delhi
What is the two-letter country code for this unit?
  [Unknown]:  IN
Is CN=Hyman Kumar, OU=Web, O=theitroad Inc., L=Delhi, ST=Delhi, C=IN correct?
  [no]:  yes

Enter key password for <svr1.theitroad.com>
        (RETURN if same as keystore password):
Re-enter new password:

第2步-获得CA签名的SSL[忽略自签名用户]

如果要使用自签名SSL证书,则不需要执行此步骤。
如果我们想从证书颁发机构购买一个有效的ssl,那么我们需要首先创建一个CSR,使用下面的命令来完成它。

创建CSR:

keytool -certreq -keyalg RSA -alias svr1.theitroad.com -file svr1.csr -keystore /etc/pki/keystore

上面的命令将提示输入密钥库密码并生成CSR文件。
使用此CSR并从任何证书颁发机构购买ssl证书。
在CA颁发证书后,我们将拥有以下文件:根证书、中间证书和证书文件。
在我的例子中文件名是

  • root.crt(根证书)
  • intermediate.crt crt(中级证书)
  • svr1.theitroad.com.crt (CA颁发的证书)

安装根证书:

keytool -import -alias root -keystore /etc/pki/keystore -trustcacerts -file root.crt

安装中级证书:

keytool -import -alias intermed -keystore /etc/pki/keystore -trustcacerts -file intermediate.crt

安装已颁发的证书:

keytool -import -alias svr1.theitroad.com -keystore /etc/pki/keystore -trustcacerts -file svr1.theitroad.com.crt

步骤3 -设置Tomcat密钥存储库

现在转到Tomcat安装目录并编辑 conf/server
文件,并按照以下方式更新配置。
如果需要,还可以将端口从8443更改为其他端口。

<Connector port="8443" protocol="HTTP/1.1"
                connectionTimeout="20000"
                redirectPort="8443"
                SSLEnabled="true"
                scheme="https"
                secure="true"
                sslProtocol="TLS"
                keystoreFile="/etc/pki/keystore"
                keystorePass="_password_" />

步骤4 -重启Tomcat

使用init脚本(如果有)重新启动tomcat服务,在我的例子中,我使用shell脚本(startup.sh和shutdown.sh)停止和启动tomcat。

./bin/shutdown.sh
./bin/startup.sh

第5步-验证设置

因为我们已经完成了tomcat设置所需的所有配置。
在第2步中配置的端口上访问浏览器中的tomcat。
本文已经在CentOS 6.5上使用Tomcat 7进行了测试,使用的是Java 8。