您可以按照官方docs 中的说明配置Netty4 组件,首先指定要使用的SSLContextParameters,它只是定义在SSL 握手期间可以找到证书的位置,然后将其设置到netty组件:
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("/users/home/server/keystore.jks");
ksp.setPassword("keystorePassword");
KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("keyPassword");
SSLContextParameters scp = new SSLContextParameters();
scp.setKeyManagers(kmp);
NettyComponent nettyComponent = getContext().getComponent("netty4", NettyComponent.class);
nettyComponent.setSslContextParameters(scp);
如果您使用 Spring (Boot),这可以在 Camel 的上下文初始化例程中轻松完成:
@Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
public void beforeApplicationStart(CamelContext camelContext) {
// code goes in here
}
@Override
public void afterApplicationStart(CamelContext camelContext) {
// noop
}
};
}
请注意,上面的组件被命名为netty4,这也应该反映在其余配置部分:
restConfiguration()
.component("netty4")
.host("0.0.0.0")
.scheme("https")
.port(8443)
...
可以看到类似的方法,只是在我的一个技术演示项目中将 Jetty 作为配置的 HTTP 服务器,它将 SSLContextParamteter configuration 保留在它自己的 bean 中,注入到 Jetty configuration 中,它只是将该参数设置到自定义的 Jetty 组件。稍后,restConfiguration 是 abstracted away into a base class,某些通过 Jetty 暴露端点的路由将从那里扩展。
进一步注意,您可以使用默认的 Jetty 或 Netty 组件。在我的演示中,我遇到了 TLS 1.0 和 1.1 客户端的错误,因为默认情况下 Jetty 9.4 排除了所有不安全的密码,并且 Camel 没有将设置正确地传播到 Jetty,但希望现在应该解决这个问题。