gpt4 book ai didi

apache-camel - 如何外部化 sslContextParameters

转载 作者:行者123 更新时间:2023-12-04 03:07:47 40 4
gpt4 key购买 nike

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

我正在使用硬编码值引用 sslContextParameters,一切正常。

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

<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 属性放置 ${},这也不起作用。

是否可以告诉我哪里出错了?

最佳答案

尝试将 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();

上下文:

<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" />

只需在您的 Camel 上下文中“ Autowiring ”它即可。

关于apache-camel - 如何外部化 sslContextParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530999/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com