我正在使用 Apache CXF (v2.7.3) 通过 HTTPS 调用 SOAP 服务。我可以从文件而不是类路径加载信任库 - 我收到“无效的 keystore 格式”错误。
我的 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 构建您的发行版,它可能会损坏二进制文件:
关于java - 如何使用 Apache CXF 从类路径加载信任库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636374/