【问题标题】:configure embedded Tomcat7 to use keystorefile embedded in executable jar配置嵌入式 Tomcat7 以使用嵌入在可执行 jar 中的 keystorefile
【发布时间】:2014-02-04 04:19:17
【问题描述】:

我已经设法将我的嵌入式 tomcat 配置为使用密钥库文件,并且当我从 eclipse 执行项目时它可以工作。

代码很简单:

...
String keystore = new File(MyServer.class.getResource("/keystore").toURI()).toPath().toString();
httpsConnector.setAttribute("keystoreFile",keystore);
...

文件keystore 位于添加到构建路径的源目录中。

将项目导出到可执行jar后,我可以验证jar根目录中keyfile的存在。

但是在执行 jar 时,我得到了这个错误:

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical

所以我假设我无法使用httpsConnector.setAttribute("keystoreFile",...) 配置密钥文件。还有另一种配置方法吗?我真的不想在临时目录中复制密钥文件并从那里引用它。

【问题讨论】:

    标签: java eclipse executable-jar keystore embedded-tomcat-7


    【解决方案1】:

    我真的不想在临时目录中复制密钥文件并从那里引用它。

    我很同情,但看起来你将不得不这样做。 Java 密钥库可以从任何类型的输入流中加载(请参阅Keystore.load),因此您会认为可以从 jar 文件中的资源加载密钥库。但是,如果您在 Tomcat 7 源代码中搜索字符串“ks.load”,您会发现它总是将 keystoreFile 属性解释为 File 的名称,并创建一个 FileInputStream,然后将其传递给 ks.load。

    因此,有必要创建包含密钥库的临时文件并将该文件的位置作为您的keystoreFile 属性传递。

    顺便说一句 - 如果您打算分发此 jar 并愿意将其保密,那么您嵌入密钥库的方法可能是可以的。

    但是,如果您计划将此 jar 分发到多个站点,则每个站点的安全性都基于相同的私钥。此外,任何有权访问 jar 文件的人都可以提取私钥,然后可以窃听或可能是中间人攻击您的所有站点。

    一般来说,最好有一个安装过程,该过程需要站点管理员生成新的私钥和证书(即使它只是一个自签名证书)。这样,每个密钥都是不同的,破坏站点的唯一方法是访问该服务器的密钥存储和密码。

    【讨论】:

      【解决方案2】:

      这可能是一个旧帖子,但我遇到了同样的问题,并没有在网上找到解决方案。所以我想分享我所做的。

      就我而言,我需要两个带有自签名证书的端口。除了在 application.yml 中配置的一个,另一个端口在主条目中配置。

      @SpringBootApplication
      public class Application {
      
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      
          @Bean
          public ServletWebServerFactory servletContainer() throws IOException
          {
              TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
              tomcat.addAdditionalTomcatConnectors(additionalConnector());
              return tomcat;
          }
      
      
          private Connector additionalConnector() throws IOException
          {
              Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
              connector.setScheme("https");
              connector.setPort(8088);
              ...
      
              URL keystore = new ClassPathResource("keystore.jks").getURL();
              connector.setAttribute("keystoreFile", keystore.toExternalForm());
              return connector;
          }
      }
      

      使用 URL,mvn spring-boot:run 和运行 jar 都可以正常加载密钥库。

      我使用的是 spring-boot 2.0.1.RELEASE 和 tomcat 8.5.29

      【讨论】:

        猜你喜欢
        • 2015-01-07
        • 2015-09-29
        • 2020-10-29
        • 2019-04-06
        • 1970-01-01
        • 2016-02-08
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多