1

使 Java 能够使用基于 PKCS#11 HSM 的密钥库的标准配置是不对密钥库路径配置任何内容(保持为空)并配置 java 安全性以获取 PKCS 11 安全性提供程序。此处提供了更多信息(使用 Sun/Oracle JVM)。Keystore 上的 Java 文档指定 - 为了创建一个空的密钥库,或者如果无法从流中初始化密钥库,请将 null 作为流参数传递。(参考:here)但是使用 Spring Boot 这样做会导致以下异常:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is
java.lang.IllegalArgumentException: Resource location must not be null
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at com.example.manager.ManagerApplication.main(ManagerApplication.java:27) [axon-manager-ws-0.3.2-SNAPSHOT.jar:0.3.2-SNAPSHOT] Caused by:
java.lang.IllegalArgumentException: Resource location must not be null
        at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:131) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:329) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSsl(TomcatEmbeddedServletContainerFactory.java:312) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.customizeSsl(TomcatEmbeddedServletContainerFactory.java:278) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.customizeConnector(TomcatEmbeddedServletContainerFactory.java:257) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:159) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
        ... 8 more

看来“org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory”类不允许密钥库路径为空。它尝试从给定路径加载资源。

Spring Boot 版本为 1.3.0。我查看了最新版本,https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory .java并且代码似乎没有更改(第 343 行) 有趣的是,当代码尝试加载信任存储时,它受到了很好的保护(允许空信任存储路径),请参见第 358 行。

这是一个简单的修复 - 检查密钥库路径是否为空和/或空并让它保持不变。

对于希望解决此问题的任何其他人,存在解决方法 - 通过覆盖“org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory”的“configureSsl”方法并将此实例配置为应用程序配置中的 EmbeddedServletContainerFactory。

问题是:是否有另一种方法可以在 Spring Boot 中配置安全/密钥库,这样我就不需要覆盖 TomcatEmbeddedServletContainerFactory?或者我应该考虑在 Spring Boot 上记录问题并建议一个补丁?

4

0 回答 0