如何使用 Apache CXF 从类路径加载信任库?

Kev*_*vin 5 java apache cxf truststore

我正在使用 Apache CXF (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>
Run Code Online (Sandbox Code Playgroud)

我可以从文件加载信任存储,但不能从类路径加载。我可以从异常中看出正在找到 truststore.jks 文件,但它无效。这是抛出异常的堆栈跟踪。

Caused by: java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:633)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:38)
at java.security.KeyStore.load(KeyStore.java:1185)
at org.apache.cxf.configuration.jsse.TLSParameterJaxBUtils.getKeyStore(TLSParameterJaxBUtils.java:142)
at org.apache.cxf.configuration.jsse.TLSParameterJaxBUtils.getTrustManagers(TLSParameterJaxBUtils.java:292)
at org.apache.cxf.configuration.jsse.TLSClientParametersConfig.createTLSClientParametersFromType(TLSClientParametersConfig.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)
Run Code Online (Sandbox Code Playgroud)

Iga*_*hko 4

我遇到了完全相同的问题,一开始就归咎于 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>
Run Code Online (Sandbox Code Playgroud)

2) 如果您使用 maven-assemble-plugin 来构建您的发行版,它可能会损坏二进制文件:

解决方案: http://jira.codehaus.org/browse/MASSEMBLY-412