gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:03:01 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com