如何设置 Apache CXF 客户端以使用 WebSphere 信任库?(接收"No trusted certificate found"例外。



首先,我将从一个总结开始。我使用Apache CXF客户机通过SSL与使用自签名证书的Apache CXF服务提供者进行通信。我将证书导入到客户端服务器上的WebSphere信任存储中,但是我仍然收到一个"javax.net.ssl.SSLHandshakeException: SSLHandshakeException调用https://somesvcprovider.com/appname/svc/myservice: com.ibm.jsse2.util.h:未找到受信任的证书"异常。

现在,这里是细节:

我有一个Apache CXF web服务客户端,我正在使用Spring对其进行配置,并且该客户端被部署到WebSphere 6.1应用服务器。CXF客户机与另一个WebSphere服务器上的Apache CXF服务提供者通信。通信使用SSL

服务提供者正在使用自签名证书。我已经通过管理控制台将提供者的证书导入到客户机服务器上的WebSphere信任库中。我通过SSL证书和密钥管理> SSL配置> NodeDefaultSSLSettings>密钥存储库和证书> NodeDefaultTrustStore>签名者证书完成了这一点;然后使用"从端口检索"工具导入证书。

然而,当我试图联系服务提供商时,我仍然收到这个错误:"javax.net.ssl.SSLHandshakeException: SSLHandshakeException调用https://somesvcprovider.com/appname/svc/myservice: com.ibm.jsse2.util.h:没有找到受信任的证书"。

Spring配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:sec="http://cxf.apache.org/configuration/security"
  xmlns:http="http://cxf.apache.org/transports/http/configuration"
  xmlns:jaxws="http://cxf.apache.org/jaxws"
  xsi:schemaLocation="
      http://cxf.apache.org/configuration/security
      http://cxf.apache.org/schemas/configuration/security.xsd
      http://cxf.apache.org/transports/http/configuration
      http://cxf.apache.org/schemas/configuration/http-conf.xsd
      http://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    <http:conduit name="*.http-conduit">
        <!-- deactivate HTTPS url hostname verification (localhost, etc) -->
        <!-- WARNING ! disableCNcheck=true should not used in production. -->
        <http:tlsClientParameters disableCNCheck="true" />
    </http:conduit>
    <!-- Read properties from property file(s). -->
    <bean id="propertyPlaceholderConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!-- The *.spring.properties files are prefixed with a system property
                    that is set on the WebSphere server. -->
                <value>classpath:spring.${my.env}.properties</value>
            </list>
        </property>
    </bean>
    <jaxws:client id="myServiceClient"
        serviceClass="com.client.stub.cxf.IMyService"
        address="${my.svc.url}" />
    <bean id="myReport" class="com.client.MyReportRequestor">
        <property name="client" ref="myServiceClient"/>
    </bean>
</beans>

如上所示,Spring通过setter注入CXF客户端。联系服务的代码如下:

List<String> formNames = client.retrieveNames(formIdsList);

另外,我不知道这是否相关,但是当我在运行时检查CXF客户机上的TLSClientParameters对象时,没有返回信任管理器。执行检查的代码如下:

// Get the trust managers for this client.
Client proxy = ClientProxy.getClient(client);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
TLSClientParameters tls = conduit.getTlsClientParameters();
TrustManager[] trustManagers = tls.getTrustManagers();  // trustManagers is null

我还需要做什么才能让Apache CXF客户端信任自签名证书吗?

我不希望在配置文件中指定信任库的路径和密码。

谢谢!

CXF可能使用了错误的SSL套接字工厂。

尝试将此添加到您的Spring配置:

<http-conf:conduit name="*.http-conduit">
    <http-conf:tlsClientParameters useHttpsURLConnectionDefaultSslSocketFactory="true"/>
</http-conf:conduit>

看看CXF和WAS是如何工作的,访问Websphere的SSLSocketFactory并使用出站拦截器将其传递给CXF是相当简单的。

如果使用以下类:

public class WebsphereSslOutInterceptor extends AbstractPhaseInterceptor<Message> {
  private String sslAlias = null;
  public WebsphereSslOutInterceptor() {
    super(Phase.SETUP);
  }
  public void handleMessage(Message message) throws Fault {
    Conduit conduit = message.getExchange().getConduit(message);
    if (conduit instanceof HTTPConduit) {
      HTTPConduit httpConduit = (HTTPConduit)conduit;
      String endpoint = (String) message.get(Message.ENDPOINT_ADDRESS);
      if (endpoint != null) {
        try {
          URL endpointUrl = new URL(endpoint);
          Map<String, String> connectionInfo = new HashMap<String, String>();
          connectionInfo.put(
            JSSEHelper.CONNECTION_INFO_REMOTE_HOST, 
            endpointUrl.getHost());
          connectionInfo.put(
            JSSEHelper.CONNECTION_INFO_REMOTE_PORT, 
            Integer.toString(endpointUrl.getPort()));
          connectionInfo.put(
            JSSEHelper.CONNECTION_INFO_DIRECTION, 
            JSSEHelper.DIRECTION_OUTBOUND);
          SSLSocketFactory factory = 
            JSSEHelper.getInstance().getSSLSocketFactory(
              sslAlias, 
              connectionInfo, 
              null);
          TLSClientParameters tlsClientParameters = httpConduit.getTlsClientParameters();
          if (tlsClientParameters != null) {
            tlsClientParameters.setSSLSocketFactory(factory);
          }
        } catch (MalformedURLException e) {
          throw new Fault(e);
        } catch (SSLException e) {
          throw new Fault(e);
        }
      }
    }
  }
  public void setSslAlias(String sslAlias) {
    this.sslAlias = sslAlias;
  }
}

然后你将能够连接到Websphere的SSLSocketFactory,并可以选择使用"动态出站端点SSL配置"设置来指定任何客户端证书,通过在jaxws:client标记中指定拦截器:

  <jaxws:client id="proxyName"
        serviceClass="proxyClass"
        address="${web.service.endpointaddress}">
    <jaxws:outInterceptors>
        <bean class="my.pkg.WebsphereSslOutInterceptor" />
    </jaxws:outInterceptors>
</jaxws:client>

另外,如果在WebsphereSslOutInterceptor中声明了sslAlias属性,则可以根据其别名选择客户端证书。

因为这是从Websphere使用SSLSocketFactory,所以信任存储也将从Websphere使用。

编辑:

我使用了CXF 2.3.6和Websphere 6.1

beny23的解决方案对我来说在WAS7上非常有效,具有以下修改(原因:httpConduit.getTlsClientParameters()可能为空):

替换该部分:

    TLSClientParameters tlsClientParameters = httpConduit.getTlsClientParameters();
    if (tlsClientParameters != null) {
      tlsClientParameters.setSSLSocketFactory(factory);
    }
与这个:

    TLSClientParameters tlsClientParameters = httpConduit.getTlsClientParameters();
    if (tlsClientParameters == null) {
      tlsClientParameters = new TLSClientParameters();
      httpConduit.setTlsClientParameters(tlsClientParameters);
    }
    tlsClientParameters.setSSLSocketFactory(factory);

我不认为您可以像使用外部组件(Apache CXF)那样使用WAS密钥存储库。您可能必须构建并使用自己的TrustManager。似乎有几个工作的例子。

相关内容