我在使用Debian 10和Tomcat作为应用服务器的Java项目中遇到了问题。
问题是当我在浏览器中使用http调用应用程序时,应用程序已经将https定义为默认协议。因此,当我尝试运行该应用程序时,我的日志文件中出现了以下错误:
org.apache.coyote.http11.AbstractHttp11Processor process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
然而,我尝试在浏览器中使用 https 协议,但连接失败并抛出错误:
以下是我的解决方案:
你需要一个证书来设置应用程序的 https 协议。首先,我必须为应用程序创建一个 keystore 文件,这更像是 https 协议的自签名证书:
sudo keytool -genkey -keyalg RSA -alias tomcat -keystore /usr/share/tomcat.keystore
注意: 您需要在服务器上安装Java才能执行此操作。可以使用 sudo apt install default-jdk
命令来安装Java。
接下来,在Tomcat服务器配置文件(/opt/tomcat/conf/server.xml
)中为应用程序添加了一个https Tomcat服务器连接器:
sudo nano /opt/tomcat/conf/server.xml
将以下内容添加到应用程序的配置中。请注意指定了密钥库文件的位置和密码。此外,定义了一个https协议的端口,该端口与http协议的端口不同:
<Connector protocol="org.apache.coyote.http11.Http11Protocol"
port="8443" maxThreads="200" scheme="https"
secure="true" SSLEnabled="true"
keystoreFile="/usr/share/tomcat.keystore"
keystorePass="my-password"
clientAuth="false" sslProtocol="TLS"
URIEncoding="UTF-8"
compression="force"
compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"/>
因此,应用程序的完整服务器配置在 Tomcat 服务器配置文件 (/opt/tomcat/conf/server.xml
) 中如下所示:
<Service name="my-application">
<Connector protocol="org.apache.coyote.http11.Http11Protocol"
port="8443" maxThreads="200" scheme="https"
secure="true" SSLEnabled="true"
keystoreFile="/usr/share/tomcat.keystore"
keystorePass="my-password"
clientAuth="false" sslProtocol="TLS"
URIEncoding="UTF-8"
compression="force"
compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"/>
<Connector port="8009" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="my-application" 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>
这次我尝试通过浏览器访问应用程序,使用的是:
https://my-server-ip-address:https-port
在我的情况下,它是这样的:
https:35.123.45.6:8443
它运行得很好。但是,我必须接受一个警告,为该网站添加了一个安全例外,因为所使用的证书是自签名的。
就这些了。
希望这有所帮助。