Web Service 主机名验证的行为差异

在 Liberty 上运行 Web Service 时,缺省情况下将通过证书来验证 HTTPS URL 中给定的主机名,而在 WebSphere Application Server Traditional 中则不是。如果主机名无效,那么 Liberty 服务器将抛出以下异常:

java.io.IOException: The https URL hostname does not match the Common Name (CN) on the server certificate in the client's truststore. Make sure the server certificate is correct, or to disable this check (NOT recommended for production) set the CXF client TLS configuration property disableCNCheck to true.

该行为差异会影响使用针对 XML Web Service 的 Java API (JAX-WS)(由 javax.xml.ws 包提供)的客户机应用程序。 如果引用了 JAX-WS API,那么项目将被标记一次。

建议的解决方案是通过正确的主机名创建服务器证书。此外,可以通过在需要禁用主机名验证的 JAX-WS 客户机上设置 http.conduit.tlsClientParameters.disableCNCheck 属性来定制验证行为。这可以通过在 server.xml 文件中添加 <webservices-bnd> 应用程序绑定,并使用 <service-ref> 元素在每个客户机上设置该属性来实现,如以下示例中所示:

<enterpriseApplication id="SimpleEcho" name="SimpleEcho"  location="SimpleEcho.ear">
  <webservices-bnd moduleName="SimpleEcho">
    <service-ref name="service/SimpleEchoService">
      <port name="SimpleEchoPort">
        <properties vendor="IBM"/>
      </port>
      <properties http.conduit.tlsClientParameters.disableCNCheck="true"/>
    </service-ref>
  </webservices-bnd>
</enterpriseApplication>

还可以通过将该属性添加到 ibm-ws-bnd.xml 文件的 <service-ref> 元素来禁用主机名验证,如以下示例中所示:

<service-ref name="service/SimpleEchoService">
  <port name="SimpleEchoPort">
    <properties vendor="IBM"/>
  </port>
  <properties http.conduit.tlsClientParameters.disableCNCheck="true"/>
</service-ref>

有关在 Liberty 上为 Web Service 客户机配置 Web Service 参考配置的详细信息,请参阅以下资源: