【问题标题】:How do I load truststore from classpath with Apache CXF?如何使用 Apache CXF 从类路径加载信任库?
【发布时间】:2013-03-16 04:58:35
【问题描述】:

我正在使用 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)

【问题讨论】:

    标签: java apache cxf truststore


    【解决方案1】:

    我遇到了完全相同的问题,一开始就责怪 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2015-04-01
      相关资源
      最近更新 更多