如何外化sslContextParameters

时间:2017-11-28 11:55:54

标签: apache-camel

我正在使用Apache Camel 2.20.0并使用带有HTTP方案的REST DSL部署REST服务。

我正在引用带有硬编码值的sslContextParameters,并且一切正常。

我找不到将资源外部化为属性文件的方法。到目前为止,我已尝试使用Camel PropertiesComponent以及Spring PropertyPlaceholderConfigurer和BridgePropertyPlaceholderConfigurer,我希望能够在配置中执行以下操作:

<camel:sslContextParameters camelContextId="camelContext1" id="routeSSLContextParameters">
    <camel:keyManagers keyPassword="{{mypassword}}">
        <camel:keyStore password="{{mypassword}}"
            resource="{{mykeystore}}" type="JKS"/>
    </camel:keyManagers>
    <camel:trustManagers>
        <camel:keyStore password="{{mypassword}}"
            resource="{{mykeystore}}" type="JKS"/>
    </camel:trustManagers>
</camel:sslContextParameters>

我也试过按照Spring属性设置$ {},这也行不通。

有可能告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:1)

尝试将BridgePropertyPlaceholderConfigurer添加到Spring Context并使用${}占位符:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <!-- bridge spring property placeholder with Camel -->
    <!-- you must NOT use the <context:property-placeholder at the same time, 
    only this bridge bean -->
    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
        <property name="location" value="classpath:com/stackoverflow/camel/labs/keys.properties" />
    </bean>

    <camel:camelContext id="exampleSsl" />  

    <camel:keyStoreParameters id="ksp" camelContextId="exampleSsl" resource="${keyStoreParameters.resource}" type="${keyStoreParameters.type}" provider="${keyStoreParameters.provider}" password="${keyStoreParamerers.password}" />
</beans>

属性文件:

keyStoreParameters.resource=/users/home/server/keystore.jks
keyStoreParameters.type=jks
keyStoreParameters.provider=jks
keyStoreParamerers.password=test

单元测试:

public class SSLPlaceholderCamelTest extends CamelSpringTestSupport {

    @Test
    public void test() {
        assertNotNull(super.context);
        KeyStoreParameters ksp = (KeyStoreParameters)super.applicationContext.getBean("ksp");
        assertThat(ksp.getType(), is("jks"));
        assertThat(ksp.getProvider(), is("jks"));
        assertThat(ksp.getResource(), is("/users/home/server/keystore.jks"));
        assertThat(ksp.getPassword(), is("test"));
    }

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("com/stackoverflow/camel/labs/SSLPlaceholderCamelTest.xml");
    }

}

编辑:

是的,我已经使用camel:sslContextParameters进行了测试,但这些属性并未被出价。您可以通过上下文和set programmatic (Setting Client Authentication On the Server Side)

访问它
KeyStoreParameters ksp = (KeyStoreParameters)context.getBean("keystore");
KeyStoreParameters tsp = (KeyStoreParameters)context.getBean("truststore");

KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("keyPassword");

SSLContextServerParameters scsp = new SSLContextServerParameters();
scsp.setClientAuthentication(ClientAuthentication.REQUIRE);
SSLContextParameters scp = (SSLContextParameters)context.getBean("sslContext");
scp.setServerParameters(scsp);
scp.setKeyManagers(kmp);

SSLContext context = scp.createSSLContext();
SSLEngine engine = scp.createSSLEngine();

context

<camel:keyStoreParameters id="keystore"
    camelContextId="exampleSsl" resource="${keyStoreParameters.resource}"
    type="${keyStoreParameters.type}" provider="${keyStoreParameters.provider}"
    password="${keyStoreParamerers.password}" />

<camel:keyStoreParameters id="trustsore"
    camelContextId="exampleSsl" resource="${keyStoreParameters.resource}"
    type="${keyStoreParameters.type}" provider="${keyStoreParameters.provider}"
    password="${keyStoreParamerers.password}" />

<camel:sslContextParameters id="sslContext" camelContextId="exampleSsl" />

Just&#34; autowire&#34;它在你的骆驼语境中。

答案 1 :(得分:0)

这就是我最后所做的:

1.使用Spring / Camel属性桥:

<bean class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer" id="springProperties">
        <property name="location" value="classpath:application.properties"/>
</bean>

2.从Spring设置SSLContext参数:

   <bean class="org.apache.camel.util.jsse.SSLContextParameters" id="routeSSLContextParameters">
        <property name="keyManagers" ref="routeKeyManagers"/>
        <property name="trustManagers" ref="routeTrustManagers"/>
        <property name="serverParameters" ref="routeSSLContextServerParameters"/>
    </bean>
    <bean class="org.apache.camel.util.jsse.KeyManagersParameters" id="routeKeyManagers">
        <property name="keyStore" ref="routeKeystore"/>
        <property name="keyPassword" value="${keyStorePassword}"/>
    </bean>
    <bean class="org.apache.camel.util.jsse.TrustManagersParameters" id="routeTrustManagers">
        <property name="keyStore" ref="routeTruststore"/>
    </bean>
    <bean class="org.apache.camel.util.jsse.SSLContextServerParameters" id="routeSSLContextServerParameters">
        <property name="clientAuthentication" value="REQUIRE"/>
    </bean>
    <keyStoreParameters id="routeKeystore" password="${keyStorePassword}" resource="${keyStoreResource}" type="JKS" xmlns="http://camel.apache.org/schema/spring"/>
    <keyStoreParameters id="routeTruststore" password="${trustStorePassword}" resource="${trustStoreResource}" type="JKS" xmlns="http://camel.apache.org/schema/spring"/>
  1. 在restconfiguration中引用SSLContext

    <restConfiguration apiContextPath="api-docs" bindingMode="json" component="restlet" contextPath="/mytest" enableCORS="true"
        host="localhost" port="9090" scheme="https">
        <endpointProperty key="sslContextParameters" value="#routeSSLContextParameters"/>.....