使 apache camel 上的 API 为 HTTPS 实施(SSL)的最佳方法是什么?

What is the most optimal way to make an API on apache camel to have (SSL) implemented for HTTPS?

我希望使使用 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")

谢谢大家!

你可以按照官方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(引导),这可以在 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 in its own bean, that is injected into the Jetty configuration which just sets that parameters onto the customized Jetty component. Later on the restConfiguration is abstracted away into a base class 某些通过 Jetty 公开端点的路由将从中扩展。

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