Java 如何使用ApacheCXF从类路径加载truststore?

Java 如何使用ApacheCXF从类路径加载truststore?,java,apache,cxf,truststore,Java,Apache,Cxf,Truststore,我正在使用ApacheCXF(v2.7.3)通过HTTPS调用SOAP服务。我可以从文件加载信任库,但不能从类路径加载-我得到“无效密钥库格式”错误 我的cfx.xml文件中有此配置: <http:conduit name="*.http-conduit"> <http:tlsClientParameters> <sec:trustManagers> <!-- For some reason, when I use the re

我正在使用ApacheCXF(v2.7.3)通过HTTPS调用SOAP服务。我可以从文件加载信任库,但不能从类路径加载-我得到“无效密钥库格式”错误

我的cfx.xml文件中有此配置:

<http:conduit name="*.http-conduit">
  <http:tlsClientParameters>
    <sec:trustManagers>
      <!--  For some reason, when I use the resource field, I get a "Invalid keystore format" exception -->
      <sec:keyStore type="JKS" password="MYPASSWORD"
                     resource="truststore.jks" />

      <!-- THIS WORKS FINE:  <sec:keyStore type="JKS" password="MYPASSWORD"
                    file="/fullPathToMyTrustStore/truststore.jks" /> -->
      </sec:trustManagers>
    </http:tlsClientParameters>
</http:conduit>

我遇到了完全相同的问题,一开始就归咎于CXF,但实际上证书在类路径上是无效的。首先要检查jar中的文件是否与项目结构中的文件相同(在打包到jar之前)

以下是可能的嫌疑犯和可能的解决方案:

1) 若您使用的是Maven,那个么过滤过程可能会损坏二进制文件(我的案例)

解决方案:从Maven筛选过程中排除证书,例如:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/*</include>
    </includes>
    <excludes>
      <exclude>**/*.jks</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
      <include>**/*.jks</include>
    </includes>
  </resource>
</resources>

src/main/resources
真的
**/*
**/*.jks
src/main/resources
假的
**/*.jks
2) 如果使用maven assembly插件构建发行版,可能会损坏二进制文件:

解决方案:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/*</include>
    </includes>
    <excludes>
      <exclude>**/*.jks</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
      <include>**/*.jks</include>
    </includes>
  </resource>
</resources>