你好堆栈溢出社区,
我正在第一次尝试(感觉像一百次)使用 Let's Encrypt 将 SSL 安装到 Tomcat9 上并开始强大(至少我相信如此),然后陷入无法解决问题的无限循环。所以在这里我恳求更有经验的人希望找到解决我问题的方法。
在我的 SSL 安装的最后步骤中,我去检查它是否在 [https://ex.example.com:8443]
工作。但是我收到以下错误:[ERR_SSL_PROTOCOL_ERROR]
.我在网上对此进行了一段时间的研究,发现论坛上有人说他们的网站正在使用 http 而不是 https,所以我试图在我的网站上查看它是否相同。我输入了网址 [http://ex.example.com:8443]
它奏效了!我认为从这里出发会很清楚,因为我肯定能够在网上找到一个简单的解决方案,但是令我沮丧的是,事实并非如此。我发现了一个 9 年前的结果,有人在他的 <connector>
中添加了一些东西。在他的server.xml
解决类似的问题。我尝试做同样的事情,但是当我尝试启动它时,我的 Tomcat 一直在崩溃,所以我将其缩小到以下范围:sslProtocol="TLS"
.我还发现其他人编辑了他们的web.xml
将所有内容重定向到 https(除了一些东西),所以我添加了(可以在下面找到)。我又做了几个小时的谷歌搜索,现在我在这里问任何可能知道我应该如何寻求帮助的人。
服务器.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" scheme="https">
<SSLHostConfig>
<Certificate certificateFile="conf/cert.pem"
certificateKeyFile="conf/privkey.pem"
certificateChainFile="conf/chain.pem" />
</SSLHostConfig>
</Connector>
web.xml<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOnly</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOrHTTP</web-resource-name>
<url-pattern>*.ico</url-pattern>
<url-pattern>/img/*</url-pattern>
<url-pattern>/css/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
最佳答案
首先,要在 Tomcat 9 上配置 TLS/SSL,您需要做的就是修改 server.xml 文件。 web.xml 文件,无论是顶级文件还是特定于 webapp 的文件,都不用于定义如何运行 TLS/SSL。 web.xml 文件仅用于在应用程序的某些部分强制执行 TLS。
现在,让我们看看您的 server.xml 文件。 Tomcat 提供了一个 sample here这显示了如何配置 SSL。
<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="${user.home}/.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS"/>
应该是这样的。这为您提供了一个等于以下内容的整体示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
这只是 Tomcat 9.x 的默认配置,已精简为必要的部分。在您的用例中,您使用的是
Http11AprProtocol
.我会简单地切换到 org.apache.coyote.http11.Http11NioProtocol
.在不相关的注释中,在您的 web.xml 中,您选择通过 HTTP(或 HTTPS)公开 CSS 和图像。我不会那样做的。没有理由这样做。从安全性甚至 SEO 的角度来看,这也是不好的做法。将其全部设为 secret 。
关于ssl - 在 http 而非 https 上运行的端口 8443,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66344593/