在带有jetty的同一服务器中使用camel创建http和https端点

问题描述

我正在尝试在我的 Web 服务之一中创建 HTTP 和 HTTPS 端点。 我希望使用 HTTPS 保护少数端点,并使用纯 HTTP 保护其他端点。

我正在使用下面的代码来做同样的事情。

    public void configure() {
        configureJetty();
        configureHttp4();
        //This works with configuring Jetty
        from("jetty:https://0.0.0.0:8085/sample1/?matchOnUriPrefix=true")
                .to("file://./?fileName=out.csv");
        //This url does not working with the configuring the jetty with configure jetty this works.
        from("jetty:http://0.0.0.0:8084/sample2/?matchOnUriPrefix=true")
                .to("file://./?fileName=out2.csv");
    }

private void configureJetty() {
        KeyStoreParameters ksp = new KeyStoreParameters();
        ksp.setResource("./trustStore.jks");
        ksp.setPassword("someSecretPassword");
        KeyManagersParameters kmp = new KeyManagersParameters();
        kmp.setKeyStore(ksp); kmp.setKeyPassword("someSecretPassword");
        SSLContextParameters scp = new SSLContextParameters();
        scp.setKeyManagers(kmp);
        JettyHttpComponent jettyComponent = getContext().getComponent("jetty",JettyHttpComponent.class);
        jettyComponent.setSslContextParameters(scp);
    }

https 在此设置下工作正常,但 http 端点不起作用。 如果我删除配置 Jetty 的方法调用,则 HTTP 端点可以工作。 如何在同一服务器中配置两者? 我不能使用弹簧靴,只能使用普通的骆驼组件。

我已经用示例代码创建了一个 github 存储库。你可以在这里找到它。 sample code

解决方法

你可以

  • 创建两个不同的 jetty 组件实例,一个用于普通 http,另一个用于 https。
  • 使用特定的别名(“jetty”和“jettys”)注册它们中的每一个
  • 在端点 uri 中使用适当的别名 "from("jettys:...")

CDI 示例:

@Produces
@ApplicationScoped 
@Named("jetty")
public final JettyHttpComponent createJettyComponent1() {       
    return this.configureJetty(false);
}

@Produces
@ApplicationScoped 
@Named("jettys")
public final JettyHttpComponent createJettyComponent2() {       
    return this.configureJetty(true);
}  

private void configureJetty(boolean ssl) {
   ...
    JettyHttpComponent jettyComponent = new JettyHttpComponent();
    if (ssl) {
        jettyComponent.setSslContextParameters(scp);
    }   
}