系统环境:
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>