Spring Boot使用https后,Tomcat连接器配置为监听端口8444时无法启动。

15
我按照指南启用了 Spring Boot 中的 https。该应用程序之前在 https://localhost:8080 上运行。

我创建了一个名为 keystore.jks 的密钥库,它与我的 application.properties 文件位于同一目录中,现在看起来像:

# Define a custom port instead of the default 8080
server.port = 8444
# Tell Spring Security (if used) to require requests over HTTPS
security.require-ssl=true
# The format used for the keystore
server.ssl.key-store-type:PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=<somepassword>
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

现在,如果我运行主方法启动Spring Boot应用程序,它会抛出:

Description:

The Tomcat connector configured to listen on port 8444 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8444, or configure this application to listen on another port.
端口未被使用,因此必须是配置错误吗?
我不确定要改变什么。这是一个简单的SPA应用程序,Spring只提供了一个index.html和一个单一的REST端点。在这种情况下,Tomcat/Spring应该如何配置才能接受https,并在没有错误的情况下启动?

server.ssl.key-store-type 行中,小字体使用 :yml 文件使用 :。而 {file}.properties 文件使用 = - oak
尝试使用调试标志运行。这可以帮助找到一些隐藏的错误。 - oak
我也遇到了同样的问题。你找到解决方案了吗? - Johna
1
是的。路径是针对项目的根目录,而不是文件夹中的相对路径。因此,在应用程序属性中,密钥的路径应为server.ssl.key-store=backend/src/main/resources/keystore.p12 - cbll
5个回答

16

我也遇到了同样的问题并且成功解决了。我的问题在于生成keystore.p12 文件。

如果你有证书文件和私钥文件,你可以使用以下命令来生成keystore.p12 文件。

openssl pkcs12 -export -in <mycert.crt> -inkey <mykey.key> -out keystore.p12 -name <alias>

您将被提示输入密码,您可以输入您喜欢的密码。 一旦生成密钥库文件,请将其复制到包含您的.jar文件的目录中。

以下是一个可行的示例配置。

server.port=8443
security.require-ssl=true
server.ssl.key-store-type=PKCS12
server.ssl.key-store=file:keystore.p12
server.ssl.key-store-password=<password>
server.ssl.key-alias=<alias>

请注意密钥存储文件路径file:keystore.p12,如果它将与可执行的.jar文件位于同一目录中。


同样的问题 - 错误的证书文件。1)验证文件的提示。如果您使用Windows,请双击*.p12文件,Windows将启动“证书导入向导”。2)还有一件事,key-alias不是必需的。 - Jack

6
我通过使用以下配置解决了相同的问题。
# Define a custom port instead of the default 8080
server.port=8443
# Tell Spring Security (if used) to require requests over HTTPS
security.require-ssl=true
# The format used for the keystore 
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=src/main/resources/keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=root0

我移除了别名,这样它就完美运行了。参考自TOMCAT SSL Error: Alias name does not identify a key entry中的"你可能不需要一个密钥别名,因为只会有一个密钥条目"。


是的,通过从application.properties中删除或注释server.ssl.key-alias=myalias,它可以正常工作。 - nix86
建议:不要在你的示例中包含密码 :) - LostAtSea
那只是一个虚拟密码 ;) - Ruthwik

3

从Spring Boot 2.0及更高版本开始,您可以忽略此属性。

security.require-ssl=true

要启用SSL,请在您的application.properties中使用以下配置:

密钥库所使用的格式

server.ssl.key-store-type=JKS

包含证书的密钥库路径

server.ssl.key-store=classpath:somecert.jks

生成证书时使用的密码

server.ssl.key-store-password=password

映射到证书的别名

server.ssl.key-alias=alias_name

注意:server.ssl.key-store指的是密钥库位置。如果在src/main/resources中存在,则使用classpath前缀。否则,请使用file:/some/location。


2

我也遇到了同样的问题,但是在我的情况下,在application.properties中密钥库文件的文件路径(在Linux上)不正确,导致出现此错误消息。


0

我也遇到了同样的问题。对我来说,server.ssl.key-alias 设置了一个错误的密钥。因此,似乎在application.properties中对服务端进行错误配置可能会导致出现此错误消息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接