问题备忘: class path resource [xx] cannot be resolved to absolute file path because

问题描述

测试服务的版本是Spring Cloud Dalston.SR5 在Spring Boot中配置https时,代码如下:

    @Bean
    @ConditionalOnExpression("#{ ${self.https.enable:false}}")
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        return tomcat;
    }

    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

        ClassPathResource resource = new ClassPathResource(".keystore");
        try {
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(port);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreFile(resource.getFile().getAbsolutePath());
            protocol.setKeystorePass(keystorePass);
            protocol.setKeyAlias(keyAlias);
            protocol.setKeyPass(keyPass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connector;
    }

复制代码

以上代码在我们idea中运行服务时,可以正常执行。但是将服务打包成可执行jar的包,以jar服务运行服务时,抛出以下错误:

java.io.FileNotFoundException: class path resource [.keystore] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/tmp/interface
e-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/.keystore
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53)
复制代码

出现以上问题的原因: 打包后Spring无法使用resource.getFile()访问JAR中的路径的文件,必须使用resource.getInputStream()。

修正代码: 通过resource.getInputStream()获取证书文件,然后存储到临时文件,最后Http11NioProtocol 实例通过这个临时文件的路径传入值,这样此Http11NioProtocol 实例可以获取此证书文件。

private Connector createSslConnector() {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

      ClassPathResource resource = new ClassPathResource(".keystore");
      // 临时目录
      String tempPath =System.getProperty("java.io.tmpdir") + System.currentTimeMillis()+".keystore";
      File f = new File(tempPath);
      logger.info(".keystore目录临时存储路径" + tempPath);

      try {
          // 将key的值转存到临时文件
          IOUtils.copy(resource.getInputStream(),new FileOutputStream(f));
          connector.setScheme("https");
          connector.setSecure(true);
          connector.setPort(port);
          protocol.setSSLEnabled(true);
          // 指向临时文件
          protocol.setKeystoreFile(f.getAbsolutePath());
          protocol.setKeystorePass(keystorePass);
          protocol.setKeyAlias(keyAlias);
          protocol.setKeyPass(keyPass);
      } catch (IOException e) {
          e.printStackTrace();
      }

      return connector;
  }

复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值