在Spring Boot应用程序中使用SSL证书

时间:2019-04-19 15:56:55

标签: spring-boot ssl ssl-certificate

我遵循了this教程,使用了自签名证书,并且该证书到目前为止一直有效。

然后,我从提供商那里购买了SSL证书,并尝试使用该证书。我收到错误消息:

2019-04-19 17:45:36.385 ERROR 9245 --- [  restartedMain] org.apache.catalina.util.LifecycleBase   : Failed to start component [Connector[HTTP/1.1-8443]]

org.apache.catalina.LifecycleException: Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1004) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:226) [tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:259) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:311) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:164) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) [spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at de.tki.chinese.ChineseApplication.main(ChineseApplication.java:24) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.2.RELEASE.jar:2.1.2.RELEASE]
Caused by: java.lang.IllegalArgumentException: DerInputStream.getLength(): lengthTag=109, too big.
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:114) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:85) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:224) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1085) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1171) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:568) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1001) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    ... 19 common frames omitted
Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
    at sun.security.util.DerInputStream.getLength(DerInputStream.java:561) ~[na:1.8.0_73]
    at sun.security.util.DerValue.init(DerValue.java:365) ~[na:1.8.0_73]
    at sun.security.util.DerValue.<init>(DerValue.java:320) ~[na:1.8.0_73]
    at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1914) ~[na:1.8.0_73]
    at java.security.KeyStore.load(KeyStore.java:1445) ~[na:1.8.0_73]
    at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:178) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:204) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.jsse.JSSEUtil.getKeyManagers(JSSEUtil.java:203) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:112) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
    ... 25 common frames omitted

2019-04-19 17:45:36.405  INFO 9245 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-04-19 17:45:36.414  INFO 9245 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

我创建了这样的密钥库:

MacBook-Pro:keystore tobias$ keytool -import -alias tomcat -file hanzien_de.key -keystore keystore_hanzien.de.p12 -storepass xxxxx

然后我在我的application.properties文件中使用了该密钥库:

# ==============================================================
# = ssh
# ==============================================================
# 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=classpath:keystore/keystore_hanzien.de.p12
#server.ssl.key-store=classpath:keystore/hanzien_de.pfx
# The password used to generate the certificate
server.ssl.key-store-password=xxxxx
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

我在做什么错了?

4 个答案:

答案 0 :(得分:0)

如果使用Maven,则可能是由于整个资源文件夹中的Maven筛选导致的。 Maven资源过滤(让您在资源文件中包含变量)会弄乱您的二进制文件-证书对修改特别敏感。

有关Maven资源过滤的更多信息:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

答案 1 :(得分:0)

首先,您似乎正在使用Java 8(8u73)。通过 8 keytool,默认为JKS 格式,而不是PKCS12。 (9 up的默认值默认为PKCS12。)这就是为什么您的异常原因与DerStuff有关;这就是为什么。 PKCS12格式是/使用DER,但不使用JKS。在-storetype pkcs12命令上指定keytool,或在应用程序配置中指定..key-store-type=JKS(最好更改名称,以免引起误解和混淆)。

第二,新密钥库(或条目)上的keytool -import仅导入证书作为“受信任证书”条目,仅可用于验证其他各方。像Tomcat之类的 TLS服务器(或过时的SSL),或者通常来说,任何证明者,必须具有“ privatekey”条目,其中包含 cert和匹配的PRIVATEKEY并且通常是链式证书。确切地说,TLS标准要求服务器发送验证实体=服务器证书所需的/所有链证书(可以选择不包括根或锚); JSSE通常会发送PrivateKeyEntry中的证书,因此您必须在其中放置所需的证书。从1990年左右开始,对于任何公共CA(例如Verisign ^ WSymantec ^ WDigicert,GoDaddy,LetsEncrypt / Identrust),至少需要一个链证书,有时需要两个,甚至很少。对于专用CA,这可能取决于CA。如果服务器未发送所需的链证书,则 some 客户端可能仍然可以验证 some 证书;特别是,浏览器通常可以“填充”来自公共CA的丢失的链证书。这会导致以下情况:与服务器的某些连接成功,而与同一服务器的其他连接失败,这往往使用户感到困惑和不安,因此不建议这样做。

如果您的.key文件实际上仅包含证书,则将其命名为.key会引起误解和混淆。如果它在PEM中包含证书和 then 密钥 ,则Java能够读取和分离证书部分,而忽略密钥;这允许keytool运行,但生成的结果文件是Tomcat无法用来接受TLS / SSL连接的。 (取决于版本和可能的配置,它可能会抛出合理的特定异常,例如“未找到密钥”或“未找到密钥”,或者它可能只是拒绝所有带有handhake_failure的连接尝试。)如果它仅包含一个密钥,或者一个密钥,然后使用证书(而不是PEM),keytool命令将失败,而您的命令显然没有失败。

keytool不能从(另一个)受支持的密钥库导入任何东西,这对您没有多大帮助,因为如果它已经在密钥库中,则不需要导入它。您的选择是:

  • 如果您有openssl命令行,请使用它来将密钥+证书转换为PKCS12。 (如果您显式提供{/ {1}},或显式指定openssl pkcs12 -export并提供或默认包含它的信任库,则{\ {1}}将包括链证书。)现有许多Stack Q和As ,可以追溯到很多年前,涵盖了这种常见和流行的选择。

  • 使用-chain来生成密钥对(已以Java支持的密钥库格式)和CSR,并获得为 CSR颁发的证书,然后使用keytool来进行以下操作之一:(1)将CA链证书导入为受信任证书,然后将服务器证书然后导入现有的私钥项,该证书会自动填写链或(2)将整个CA链直接导入到现有私钥条目。该替代方案上也有许多现有的问与答,以及Sun / Oracle自己的Java文档,以及每个CA定制的版本(或差不多)。

  • 编写或查找并使用一个程序,该程序可以将所需要的任何格式的私钥和证书显式加载到支持的密钥库中的“ privatekey”条目中。这是更多的工作,并且只有几个问与答。

答案 2 :(得分:0)

在带有PCKS12证书启动的Spring Boot Application中遇到此错误。只需将Java版本从8升级到11,即可修复该错误。请记住,您使用的所有类都必须同时在两个Java版本中使用。

答案 3 :(得分:0)

直接从cmd运行

java -jar -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.trustStore=C:\Users\certificates\Cert_name -Dspring.profiles.active=local target\jar_or_war_name_with_dot_extension