【问题标题】:What is the most optimal way to make an API on apache camel to have (SSL) implemented for HTTPS?在 apache camel 上创建 API 以实现 HTTPS 的最佳方法是什么?
【发布时间】:2019-04-03 14:28:47
【问题描述】:

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

谢谢大家!

【问题讨论】:

    标签: java apache maven apache-camel netty


    【解决方案1】:

    您可以按照官方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 组件。稍后,restConfigurationabstracted away into a base class,某些通过 Jetty 暴露端点的路由将从那里扩展。

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

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多