在 Apache Tomcat 服务器上启用 HTTPS 或 SSL 正确方式的分步指南 – 端口 8443
已发表: 2021-04-03我开始使用 Apache Tomcat 已经快 12 年了。 我相信当我做我的第一个低年级项目时,它是在 Tomcat 版本1.x
上。 现在它已经在8.0
版本上。 在我的日常工作生活中,我大部分时间都在与 Tomcat Server 保持联系,没有它就活不下去。
在 Crunchify 上,我们已经发表了近 40 篇关于Apache Tomcat
的文章。 在大部分教程中,我在端口 8080 上运行服务器,这是server.xml
文件中配置的默认端口。
在本教程中,我们将详细介绍how to enable HTTPS/SSL on Apache Tomcat Server
的所有步骤。
让我们开始吧:
第1步。 创建密钥库
keytool
:我们将使用keytool
命令生成安全密钥——这是密钥和证书管理工具。
命令:
1 2 3 4 |
bash - 3.2 $ pwd / Users / Shared bash - 3.2 $ keytool - genkey - alias crunchify - keyalg RSA - keystore / Users / Shared / crunchify . keystore |
文件将在文件夹/Users/Shared
下创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
bash - 3.2 $ keytool - genkey - alias crunchify - keyalg RSA - keystore / Users / Shared / crunchify . keystore Enter keystore password : Re - enter new password : What is your first and last name ? [ Unknown ] : App Shah What is the name of your organizational unit ? [ Unknown ] : Crunchify , LLC What is the name of your organization ? [ Unknown ] : Crunchify What is the name of your City or Locality ? [ Unknown ] : New York City What is the name of your State or Province ? [ Unknown ] : NY What is the two - letter country code for this unit ? [ Unknown ] : US Is CN = App Shah , OU = "Crunchify, LLC" , O = Crunchify , L = New York City , ST = NY , C = US correct ? [ no ] : yes Generating 2 , 048 bit RSA key pair and self - signed certificate ( SHA256withRSA ) with a validity of 90 days for : CN = App Shah , OU = "Crunchify, LLC" , O = Crunchify , L = New York City , ST = NY , C = US |
我正在使用 Mac OS X,因此如果您在 Windows 上,请相应地替换您的路径。 我使用了密码123456
。
第2步。 创建自签名证书签名请求
1 2 |
bash - 3.2 $ keytool - certreq - keyalg RSA - alias crunchify - file crunchify . csr - keystore crunchify . keystore Enter keystore password : |
输入密码:123456
步骤 3。 让我们首先检查一下 Tomcat 是否通过 HTTP 在端口 8080 上运行。
使用命令Start
tomcat 服务器:< startup.sh
>/bin/startup.sh。
确保您位于正确的 Tomcat 路径位置。
1 2 3 4 5 6 7 8 9 10 11 |
bash - 3.2 # pwd / Users / Shared / apache - tomcat - 9.0.30 / bin bash - 3.2 $ . / startup . sh Using CATALINA_BASE : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_HOME : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_TMPDIR : / Users / Shared / apache - tomcat - 9.0.30 / temp Using JRE_HOME : / Library / Java / JavaVirtualMachines / jdk - 13.0.1.jdk / Contents / Home Using CLASSPATH : / Users / Shared / apache - tomcat - 9.0.30 / bin / bootstrap . jar : / Users / Shared / apache - tomcat - 9.0.30 / bin / tomcat - juli . jar Tomcat started . |
点击 URL: http://localhost:8080
以确保服务器已启动并正在运行。
第四步。 现在通过 HTTPS 检查端口 8443 上的 tomcat 实例。
- 现在检查端口 8443(HTTPS/SSL URL)。
- 点击网址:
https://localhost:8443
- 您应该会看到错误消息。 页面根本不应该加载。
步骤 5。 更新 /conf/server.xml 文件
由于我们在上面生成了密钥库和证书签名请求,我们需要告诉 tomcat 使用它。
- 打开位于
<tomcat_home>/conf/
文件夹的server.xml
文件并修改设置。 - 在我们的例子中,它是
/Users/Shared/apache-tomcat-9.0.30/conf
文件夹。
将以下部分直接添加到 server.xml 文件中:

1 2 3 4 5 |
< Connector port = "8443" protocol = "HTTP/1.1" SSLEnabled = "true" maxThreads = "150" scheme = "https" secure = "true" keystoreFile = "/Users/Shared/crunchify.keystore" keystorePass = "123456" clientAuth = "false" sslProtocol = "TLS" sslVerifyClient = "optional" sslEnabledProtocols = "TLSv1.2,TLSv1.1,SSLv2Hello" / > |
- 这里的密码是我在
Step-1
中使用的123456
。 - 协议:HTTP/1.1
保存存档。 接下来我们将停止并启动 Tomcat 服务器。
步骤 6。 停止和启动 Apache Tomcat。
- bash-3.2$ /Users/Shared/apache-tomcat-9.0.30/bin/shutdown.sh
- bash-3.2$ /Users/Shared/apache-tomcat-9.0.30/bin/startup.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
bash - 3.2 $ / Users / Shared / apache - tomcat - 9.0.30 / bin / shutdown . sh Using CATALINA_BASE : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_HOME : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_TMPDIR : / Users / Shared / apache - tomcat - 9.0.30 / temp Using JRE_HOME : / Library / Java / JavaVirtualMachines / jdk - 13.0.1.jdk / Contents / Home Using CLASSPATH : / Users / Shared / apache - tomcat - 9.0.30 / bin / bootstrap . jar : / Users / Shared / apache - tomcat - 9.0.30 / bin / tomcat - juli . jar NOTE : Picked up JDK_JAVA_OPTIONS : -- add - opens = java . base / java . lang = ALL - UNNAMED -- add - opens = java . base / java . io = ALL - UNNAMED -- add - opens = java . rmi / sun . rmi . transport = ALL - UNNAMED bash - 3.2 $ / Users / Shared / apache - tomcat - 9.0.30 / bin / startup . sh Using CATALINA_BASE : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_HOME : / Users / Shared / apache - tomcat - 9.0.30 Using CATALINA_TMPDIR : / Users / Shared / apache - tomcat - 9.0.30 / temp Using JRE_HOME : / Library / Java / JavaVirtualMachines / jdk - 13.0.1.jdk / Contents / Home Using CLASSPATH : / Users / Shared / apache - tomcat - 9.0.30 / bin / bootstrap . jar : / Users / Shared / apache - tomcat - 9.0.30 / bin / tomcat - juli . jar Tomcat started . |
步骤 7。 检查 HTTPS url 和认证:https://localhost:8443
现在再次点击 HTTPS 安全 URL 以检查您的页面是否已成功加载。
铬合金:
- 默认情况下,
Chrome Blocks all insecure HTTPS
站点。 - 启用
Chrome flag
以加载从 localhost 加载的资源的无效证书
- 转到 Chrome
- 打开标签
- 类型:
chrome://flags/#allow-insecure-localhost
- 从下拉列表中启用标志
- 重新启动 Chrome
Safari 和 Mozilla 火狐:
-
Accept invalid certificate error
并访问站点。
您应该看到 Apache Tomcat 页面通过 HTTPS 在端口 8443 上加载
你都准备好了。 你注意到red padlock
了吗? 别担心。 我们没有从 Verisign 或 Comodo 购买 SSL 证书。
在生产环境中,您可能看不到那个红十字标志。
单击浏览器中的证书,我们的默认自签名证书应
valid for 90
天。
奖励点:
如何使用命令keytool
检查您的cert content
?
1 |
bash - 3.2 # keytool -list -keystore /Users/<username>/crunchify.keystore |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
bash - 3.2 $ keytool - list - keystore / Users / Shared / crunchify . keystore Enter keystore password : Keystore type : PKCS12 Keystore provider : SUN Your keystore contains 1 entry crunchify , Apr 24 , 2020 , PrivateKeyEntry , Certificate fingerprint ( SHA - 256 ) : 9D : 26 : 77 : E0 : E8 : 79 : 5B : 46 : 57 : 64 : 04 : 95 : B8 : 22 : 9C : E2 : 9F : A4 : 82 : CB : 01 : B5 : 80 : 62 : 75 : 90 : 48 : 27 : 5F : 60 : CB : C8 bash - 3.2 $ |