搜索

28.20. 高级用法

download PDF

如果您需要对 HTTP 生成者进行更多控制,您应该使用 HttpComponent,您可以在其中设置各种类来为您提供自定义行为。

28.20.1. 为 HTTP 客户端设置 SSL

使用 JSSE 配置工具

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

组件的编程配置

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("https", 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="https://127.0.0.1/mail/?sslContextParameters=#sslContextParameters"/>

直接配置 Apache HTTP 客户端

基本上 camel-http 组件基于 Apache HttpClient 构建。详情请参阅 SSL/TLS 自定义,或查看 org.apache.camel.component.http.HttpsServerTestSupport 单元测试基本类。
您还可以实施自定义 org.apache.camel.component.http.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("http", HttpComponent.class);
httpComponent.setHttpClientConfigurer(new MyHttpClientConfigurer());

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

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

<to uri="https://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.引用 http URL 中的上下文:
<to uri="https://myhostname.com:443/myURL?httpContext=myHttpContext"/>

使用不同的 SSLContext 参数

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

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

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

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.