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

标签 java apache maven apache-camel netty

我希望使用 Apache-Camel 创建的 API 能够启用 HTTPS。我已经对各种方法(使用 Jetty、Netty 等)进行了一些阅读,但我想知道为我的基于 Camel 的 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(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,这也应该反射(reflect)在其余配置部分中:

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

可以看到类似的方法,只是在我的一个技术演示项目中使用 Jetty 作为配置的 HTTP 服务器,该项目保留 SSLContextParamteter configuration在它自己的 bean 中,注入(inject) Jetty configuration它只是将参数设置到定制的 Jetty 组件上。稍后 restConfigurationabstracted away into a base class通过 Jetty 公开端点的某些路由将从其中扩展。

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

关于java - 在 apache Camel 上为 HTTPS 实现 API(SSL)的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55497605/

相关文章:

java - StdIn 不工作... @ Linux

java - 在方法中重构多个 if-else 条件

maven - Intellij Idea Maven 构建 "failed to read artifact descriptor"

python - 目标 WSGI 脚本 '/opt/python/current/app/.../wsgi.py' 无法作为 Python 模块加载

apache - 如何在 Centos 7 中更改虚拟主机的 apache 用户?

angularjs - 使用 Maven、Protractor 和 Selenium WebDriver 进行集成测试

java - 在受限 (Citrix) 环境中从 Java 调用 .bat 文件

java - @RequestMapping ("/main")在 netbeans spring 4 中不起作用,但在 spring 3 中起作用

java - 如何将java连接到网站/虚拟主机数据库

java - 如何解决无法在 Maven 项目中内省(introspection)测试类的警告?