44.21. 高级用法
如果您需要对 HTTP 生成者进行更多控制,您应该使用 HttpComponent,您可以在其中设置各种类来为您提供自定义行为。
44.21.1. 为 HTTP 客户端设置 SSL 复制链接链接已复制到粘贴板!
使用 JSSE 配置实用程序
HTTP 组件通过 Camel JSSE 配置实用程序 支持 SSL/TLS 配置实用程序。这个工具大大减少了您需要写入的组件特定代码数量,并在端点和组件级别进行配置。
以下示例演示了如何将 实用程序与 HTTP 组件一起使用。
组件的编程配置
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("file:/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="file:/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 route builder 类中可以 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 来解决:
为 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 }在 Spring 应用上下文文件中声明一个 HttpContext。
<bean id="myHttpContext" factory-bean="httpContextFactory" factory-method="getObject"/>- 引用 http URL 中的上下文:
<to uri="https://myhostname.com:443/myURL?httpContext=myHttpContext"/>
使用不同的 SSLContextParameters
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>
有关如何配置 SSL 的更多信息,请参阅 http-ssl 示例。