1. 程式人生 > >Apache Camel配置SSL的rest

Apache Camel配置SSL的rest

系統環境:

java version "1.8.0_66"

apache camel: 2.15.2

關於rest元件的用法,請參照:http://blog.csdn.net/mn960mn/article/details/48372565

那麼,如何配置ssl呢,請繼續往下看

一:建立證書

1:建立金鑰對

keytool -genkeypair -alias rest -keystore restlet.jks

然後,按照提示,一步一步輸入證書資訊

2:匯出公鑰證書

keytool -exportcert -alias rest -keystore restlet.jks -file client.cer

3:把上一步匯出的證書匯入到瀏覽器中(可選)

二:在camel裡配置ssl的rest,有兩種方法

1:

<camel:sslContextParameters id="restSSLContextParameters">
	<camel:keyManagers keyPassword="123456">
		<camel:keyStore password="123456" type="jks" resource="E:/cert/restlet.jks" />
	</camel:keyManagers>
</camel:sslContextParameters>

<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
	<restConfiguration component="restlet" contextPath="/" port="7722" scheme="https">
		<!-- 注意這個value的值,以#號開頭 -->
		<endpointProperty key="sslContextParameters" value="#restSSLContextParameters" />
	</restConfiguration>
	<rest>
		<get uri="/ssl/test">
			<to uri="direct:aaa" />
		</get>
	</rest>
	<route>
		<from uri="direct:aaa" />
		<setBody>
			<simple>this is ssl body</simple>
		</setBody>
	</route>
</camelContext>


2:

<camel:sslContextParameters id="restSSLContextParameters">
	<camel:keyManagers keyPassword="123456">
		<camel:keyStore password="123456" type="jks" resource="E:/cert/restlet.jks" />
	</camel:keyManagers>
</camel:sslContextParameters>

<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
	<restConfiguration component="restlet" contextPath="/" port="7722" scheme="https" />
	<route>
		<from uri="rest:get:/ssl/test?sslContextParameters=#restSSLContextParameters" />
		<setBody>
			<constant>this is rest ssl body</constant>
		</setBody>
	</route>
</camelContext>


然後,啟動程式,就可以直接訪問

https://127.0.0.1:7722/ssl/test

以上是單向認證。

如果需要雙向認證的話,則還需要把客戶端證書配置上去,示例程式碼如下:

<camel:sslContextParameters id="restSSLContextParameters">
	<camel:keyManagers keyPassword="123456">
		<camel:keyStore password="123456" type="jks" resource="E:/cert/restlet.jks" />
	</camel:keyManagers>
	<camel:trustManagers>
		<camel:keyStore password="xxx" resource="xxxx"/>
	</camel:trustManagers>
</camel:sslContextParameters>