150.18. 高级使用

如果您需要更多对 HTTP 生产者的控制,您应该在什么位置使用 HttpComponent 来提供自定义行为。

150.18.1. 为 HTTP 客户端设置 SSL

使用 JSSE 配置实用程序

从 Camel 2.8 开始,HTTP4 组件支持通过 Camel JSSE 配置实用程序 的 SSL/TLS 配置。  这个实用程序可大大减少您需要编写的组件特定代码的数量,并在端点和组件级别进行配置。  以下示例演示了如何将 实用程序与 HTTP4 组件搭配使用。

组件的程序配置

KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("/users/home/server/keystore.jks");
ksp.setPassword("keystorePassword");

KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("keyPassword");

SSLContextParameters scp = new SSLContextParameters();
scp.setKeyManagers(kmp);

HttpComponent httpComponent = getContext().getComponent("https4", HttpComponent.class);
httpComponent.setSslContextParameters(scp);

基于 Spring DSL 端点配置

...
  <camel:sslContextParameters
      id="sslContextParameters">
    <camel:keyManagers
        keyPassword="keyPassword">
      <camel:keyStore
          resource="/users/home/server/keystore.jks"
          password="keystorePassword"/>
    </camel:keyManagers>
  </camel:sslContextParameters>...
...
  <to uri="https4://127.0.0.1/mail/?sslContextParameters=#sslContextParameters"/>...

直接配置 Apache HTTP 客户端

基本上是 camel-http4 组件基于 Apache HttpClient 构建的。请参考 SSL/TLS 自定义 以获取详细信息,或查看 org.apache.camel.component.http4.HttpsServerTestSupport 单元测试基础类。
您还可以实施自定义的 org.apache.camel.component.http4.HttpClientConfigurer 在 http 客户端上进行一些配置(如果您需要完全控制它)。

但是,如果您只想 指定密钥存储和信任存储,您可以使用 Apache HTTP HttpClientConfigurer 进行此操作,例如:

KeyStore keystore = ...;
KeyStore truststore = ...;

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 443, new SSLSocketFactory(keystore, "mypassword", truststore)));

然后,您需要创建一个实现 HttpClientConfigurer 的类,并注册 https 协议,该协议根据上例提供密钥存储或信任存储。然后,从 camel 路由构建器类中,您可以进行 hook,如下所示:

HttpComponent httpComponent = getContext().getComponent("http4", HttpComponent.class);
httpComponent.setHttpClientConfigurer(new MyHttpClientConfigurer());

如果使用 Spring DSL 执行此操作,您可以使用 URI 指定 HttpClientConfigurer。例如:

<bean id="myHttpClientConfigurer"
 class="my.https.HttpClientConfigurer">
</bean>

<to uri="https4://myhostname.com:443/myURL?httpClientConfigurer=myHttpClientConfigurer"/>

只要您实施 HttpClientConfigurer,并且按照上述步骤配置密钥存储和信任存储,它将正常工作。

使用 HTTPS 验证 getchas

最终用户报告,他使用 HTTPS 进行身份验证时出现问题。这个问题最终已通过提供自定义配置的 org.apache.http.protocol.HttpContext 来解决:

  • 1. 为 HttpContexts 创建(Spring)工厂:
public class HttpContextFactory {

  private String httpHost = "localhost";
  private String httpPort = 9001;

  private BasicHttpContext httpContext = new BasicHttpContext();
  private BasicAuthCache authCache = new BasicAuthCache();
  private BasicScheme basicAuth = new BasicScheme();

  public HttpContext getObject() {
    authCache.put(new HttpHost(httpHost, httpPort), basicAuth);

    httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    return httpContext;
  }

  // getter and setter
}
  • 2.在 Spring 应用程序上下文文件中声明 HttpContext:
<bean id="myHttpContext" factory-bean="httpContextFactory" factory-method="getObject"/>
  • 3.引用 http4 URL 中的上下文:
<to uri="https4://myhostname.com:443/myURL?httpContext=myHttpContext"/>

使用不同的 SSLContextParameters

HTTP4 组件只支持每个组件一个 org.apache.camel.util.jsse.SSLContextParameters 实例。如果您需要使用 2 个或更多不同的实例,则需要设置多个 HTTP4 组件,如下所示。如果我们有 2 个组件,每个组件都使用自己的 sslContextParameters 属性实例。

<bean id="http4-foo" class="org.apache.camel.component.http4.HttpComponent">
   <property name="sslContextParameters" ref="sslContextParams1"/>
   <property name="x509HostnameVerifier" ref="hostnameVerifier"/>
</bean>

<bean id="http4-bar" class="org.apache.camel.component.http4.HttpComponent">
   <property name="sslContextParameters" ref="sslContextParams2"/>
   <property name="x509HostnameVerifier" ref="hostnameVerifier"/>
</bean>