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

托比

我按照教程使用自签名证书进行了学习,到目前为止该方法仍然有效。

然后,我从提供商那里购买了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

我究竟做错了什么?

dave_thompson_085

首先,您似乎正在使用Java 8(8u73)。通过8keytool默认为JKS格式,而不是PKCS12。(9 up的确默认为PKCS12。)这就是为什么您的异常原因与DerStuff有关;这是为什么?PKCS12格式是/使用DER,但不使用JKS。请指定-storetype pkcs12keytool命令,或指定..key-store-type=JKS在您的应用程序配置(最好更改名称,以便它不主动误导和混乱的人)。

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

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

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

  • 如果您有openssl命令行,请使用它来将密钥+证书转换为PKCS12。openssl pkcs12 -export如果您显式提供链证书,或者明确指定-chain并提供或默认包含它的信任库,则将包括链证书。)有数十种现有的Stack Q和As,可以追溯到很多年前,涵盖了这一共同点和流行的选择。

  • 使用keytool生成密钥对(已在支持Java密钥存储格式)和企业社会责任,并获得颁发的证书CSR,然后使用keytool -import到:(1)进口CA链证书的信任,然后在服务器证书现有的专用密钥进入,它会自动填充链,或者(2)将整个CA链直接导入现有的私钥条目此替代方案上也有许多现有的问与答,以及Sun / Oracle自己的Java文档,以及每个CA定制的版本(或差不多)。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用SSL运行Spring Boot应用程序

多个 ssl 证书(多个域)到同一个 Spring Boot 应用程序

在Spring Boot应用程序项目中使用Wildfly

在非Spring-Boot应用程序中使用@ConfigurationProperties

在Spring Boot应用程序中使用@Configuration访问配置

在 Spring Boot 应用程序中使用 Soap 服务

如果您在 docker 中使用了 Spring Boot 应用程序,那么使用 SSL 的最佳方法是什么

在Spring Boot应用程序的应用程序属性文件中使用Jenkins变量

使用P7B证书在Spring Boot Java应用程序中启用HTTPS

在独立应用程序中使用Spring

如何配置Spring Boot应用程序以通过MySQL使用SSL / TLS?

如何在使用注释的Spring Boot应用程序中使用ibatis?

在使用JHipster创建的Spring Boot应用程序中使用npm install时出错

在Spring Boot MVC应用程序中使会话无效(注销)

确定在Spring Boot应用程序中使用哪种实现

如何在JavaFX应用程序中使用Spring Boot构建插件

在Spring Boot应用程序中使用Azure KeyVault作为数据库密码

如何在Spring Boot Data Jpa应用程序中使用条件查询

在简单的Spring Boot应用程序中使用了哪个ApplicationContext实现?

如何在Spring Boot应用程序中使用JPQL配置JPA-“无法解析符号..”

在这个 java spring boot 应用程序中使用 logstash 时,filebeats 是如何被调用的?

在 spring-boot 应用程序中使用 Micrometer 和 OpenFeign

如何在Spring Boot应用程序中使用Thymeleaf加载HashMap和ModelandView对象值?

在log4j2.xml中使用Spring Boot应用程序属性

如何在派生进程中使用gradle任务启动spring boot应用程序?

在Spring Boot应用程序中使用Rackspace时的Apache jclouds java.lang.NoSuchMethodError

如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性?

无法在Spring Boot应用程序的Main方法中使用@Autowired JPA存储库

在Spring Boot应用程序中允许在哪些类中使用@Autowired?