在 apache Camel 上使 API 为 HTTPS 实现 (SSL) 的最佳方法是什么?



我希望让我用Apache-Camel创建的API启用HTTPS。我已经对各种方法(使用Jetty,Netty等)进行了一些阅读,但我想知道在基于骆驼的API上实现SSL的最简单,最有效的方法是什么。这是我目前的配置,我更喜欢(为了简单起见,如果我可以使用netty4-http)

public void configure() {
restConfiguration()
.component("netty4-http")//Specifies the Camel component to use as the REST transport
.host("0.0.0.0")//The hostname to use for exposing the REST service
.port(8080).bindingMode(RestBindingMode.auto)
.rest("/v1/API.Endpoint")

谢谢大家!

您可以按照官方文档中提到的配置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(引导),这可以很容易地在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,这也应该反映在 rest 配置部分:

restConfiguration()
.component("netty4")
.host("0.0.0.0")
.scheme("https")
.port(8443)
...

可以看到类似的方法,只是在我的一个技术演示项目中将 Jetty 作为配置的 HTTP 服务器,该项目将 SSLContextParamteter 配置保存在自己的 bean 中,该配置被注入到 Jetty 配置中,该配置只是将该参数设置到自定义的 Jetty 组件上。稍后,restConfiguration被抽象为基类,通过 Jetty 公开终结点的某些路由将从基类扩展。

进一步请注意,您可以使用默认的 Jetty 或 Netty 组件。在我的演示中,我在TLS 1.0和1.1客户端上遇到了一个错误,该错误无法连接,因为默认情况下Jetty 9.4排除了所有不安全的密码,并且Camel没有将设置正确传播到Jetty,希望现在应该解决。

相关内容

最新更新