【问题标题】:Spring boot embedded tomcat not loading SSL keystore file from classpathSpring Boot 嵌入式 tomcat 未从类路径加载 SSL 密钥库文件
【发布时间】:2016-01-30 12:01:31
【问题描述】:

我正在为我的应用程序使用 Spring boot 1.2.7,作为要求,我必须从类路径加载 SSL 证书。因此,我在类路径中添加了我的 PKCS12 文件并使用以下代码(在 AppInitializer 类中加载它:

final String sslKeystoreFilepath = this.getClass().getClassLoader().getResource(sslKeystoreFilename).getFile();

我在这里注意到两件事:

  1. 字符串中包含file:。如果我使用main 方法通过一个普通的独立程序运行相同的代码,我看不到像file: 这样的任何东西。

  2. 当我运行 Spring boot 应用程序(使用生成的 fat jar)时,它会抛出异常:

SEVERE: Failed to load keystore type PKCS12 with path /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 due to /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 (No such file or directory) java.io.FileNotFoundException: /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 (No such file or directory)

我做错了什么?

【问题讨论】:

  • 如何将文件添加到类路径中?
  • 将文件放到src/main/resources 中,该文件将被捆绑在 JAR 文件中。我在这里做错什么了吗?请建议是否有其他方法。

标签: java spring-boot ssl-certificate


【解决方案1】:

我认为您可以在 .properties 文件中添加此配置,如下所示,而不是获取字符串,

server.ssl.key-store: classpath:dev_keystore.p12
server.ssl.key-store-password: mypassword
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

如果你需要做你现在正在做的事情,请告诉我,我会给出答案

【讨论】:

  • 出于我们应用的设计考虑,我不能这样做。
【解决方案2】:

answer 中似乎 Tomcat 无法读取包含在 jar 中的密钥库,但我不确定,因为我使用 Spring Boot 1.2.5,并且在类路径中的密钥库中,一切都像魅力一样. 但是,当我尝试升级到 Spring Boot 1.2.7 及更高版本时,我遇到了与您完全相同的错误,所以我得出结论,这取决于 Spring Boot。 这时候,我降级到了1.2.5版本。 :(

【讨论】:

  • 这听起来很有趣。因为我的是一个新应用程序,我开始使用 Spring Boot 1.2.6。如果它适用于 1.2.5 而不是最新版本,我认为您应该在他们的 JIRA 中创建一个问题票,不是吗?
【解决方案3】:

我不确定这是否会对某人有所帮助,但在 Spring 1.5.8 上,如果您已将密钥放入项目根级别,只需将其添加到您的 application.yml 中:

server:
  ssl:
    key-store-type: PKCS12
    key-store: keystore.p12
    key-store-password: some_password
    key-alias: tomcat

它会毫无问题地工作。

【讨论】:

    【解决方案4】:

    这适用于我从带有 Spring Boot 2.0.2.RELEASE 的 JAR 中运行。

    @Bean public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        ...
        return tomcat;
    }
    
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
    
        connector.setScheme("https");
        connector.setSecure(true);
        connector.setPort(8443);
    
        protocol.setSSLEnabled(true);
        protocol.setKeystoreType("pkcs12");
        protocol.setKeystorePass("changeit");
        protocol.setKeyAlias("tomcat");
    
        try {
            ClassPathResource keystoreResource = new ClassPathResource("keystore");
            URL keystoreUrl = keystoreResource.getURL();
            String keystoreLocation = keystoreUrl.toString();
            protocol.setKeystoreFile(keystoreLocation);
        }
        catch (IOException ex) {
            throw new IllegalStateException("can't access keystore: [keystore]", ex);
        }
    
        return connector;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 2018-12-19
      • 2015-06-11
      • 1970-01-01
      • 2018-01-08
      • 2021-08-31
      • 2018-02-22
      • 2020-06-05
      相关资源
      最近更新 更多