Camel——http4组件https使用
camel配置pom文件
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http4</artifactId>
<version>2.17.1</version>
</dependency>
使用springcamel的DSL实现https配置,配置xml如下:
<!-- 配置ssl证书 -->
<camel:sslContextParameters id="sslContextParameters">
<camel:trustManagers>
<camel:keyStore resource="ca.jks"
password="123456" type="JKS" />
</camel:trustManagers>
<camel:keyManagers keyPassword="IoM@1234">
<camel:keyStore resource="CertwithKey.pkcs12"
password="123456" type="PKCS12" />
</camel:keyManagers>
</camel:sslContextParameters>
<bean id="https4" class="org.apache.camel.component.http4.HttpComponent">
<property name="sslContextParameters" ref="sslContextParameters" />
</bean>
配置route,做一个登陆认证的流程,route配置如下:
<route id="login">
<from id="1" uri="timer://foo5?fixedRate=true&period=60000&repeatCount=1" />
<to id="2" uri="bean:com.process.login.LoginRequestProcess" />
<to id="3" uri="https4://{{platform.url}}/?x509HostnameVerifier=#allowAllHostnameVerifier" />
<convertBodyTo charset="UTF-8" id="4" type="String" />
<to id="5" uri="bean:com.process.login.LoginResponseProcess" />
<log id="_log12" logName="login" message="登录${body} " />
</route>
上图中https4配置参数x509HostnameVerifier=#allowAllHostnameVerifier,其作用是不验证证书的域名。
配置allowAllHostnameVerifier参数需在camel的xml中添加bean配置:
<bean id="allowAllHostnameVerifier" class="org.apache.http.conn.ssl.AllowAllHostnameVerifier"/>
如果配置值参数:运行错误如下:
2018-09-05 18:00:15.433 [Camel (camelContext-2dbf370a-2c54-4112-bda1-6939520839db) thread #0 - timer://foo5] WARN o.a.c.component.timer.TimerConsumer -Error processing exchange. Exchange[ID-DESKTOP-1666376-56398-1536141613101-0-2]. Caused by: [javax.net.ssl.SSLPeerUnverifiedException - Host name '10.10.20.11' does not match the certificate subject provided by the peer (CN=demo.com, OU=CN, O=Huawei, L=SZ, ST=GD, C=CN)]
javax.net.ssl.SSLPeerUnverifiedException: Host name '10.10.20.11' does not match the certificate subject provided by the peer (CN=demo.com, OU=CN, O=Huawei, L=SZ, ST=GD, C=CN)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:465) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.camel.component.http4.HttpProducer.executeMethod(HttpProducer.java:301) ~[camel-http4-2.17.1.jar:2.17.1]
at org.apache.camel.component.http4.HttpProducer.process(HttpProducer.java:173) ~[camel-http4-2.17.1.jar:2.17.1]
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) [camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) ~[camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) [camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192) [camel-core-2.17.1.jar:2.17.1]
at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76) [camel-core-2.17.1.jar:2.17.1]
at java.util.TimerThread.mainLoop(Unknown Source) [na:1.8.0_91]
at java.util.TimerThread.run(Unknown Source) [na:1.8.0_91]
当Content-type为application/x-www-form-urlencoded
需设置body数据格式:a=xxx&bb=XXX