java – 如何使用Apache CXF从classpath加载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>

我可以从文件加载信任存储,但不能从类路径加载.我可以从异常中看出找到了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)

最佳答案 我有完全相同的问题,并在开始时归咎于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>

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

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

点赞