【发布时间】:2020-05-04 15:39:15
【问题描述】:
我使用 Apache Camel 公开了一个简单的 REST 服务,例如 Spring boot 微服务,它使用 netty4-http 组件在 https 中创建对服务的请求。
public class RoutingTest extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.host("localhost")
.port("8080");
rest().post("test")
.route()
.setBody(constant("message=Hello"))
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.POST))
.setHeader(Exchange.CONTENT_TYPE, constant("application/x-www-form-urlencoded"))
.to("netty4-http:https://localhost/service/test");
}
}
当我调用http://localhost:8080/test 时,我在路由调用https://localhost/service/test 服务时收到 400 Bad Request 错误。从日志中我读到请求以 HTTP 而不是 HTTPS 格式到达,我不明白为什么:
您对启用 SSL 的服务器端口使用纯 HTTP。而是使用 请使用 HTTPS 方案访问此 URL。
如果我使用 Postman 调用服务 https://localhost/service/test,它可以正常工作。
SSL 配置了自签名证书。
如何使用 apache camel 中的 netty 组件创建正确的 https 请求?文档仅建议更换协议,最多有几个选项不起作用。
更新(已解决见下文)
我用这种方式更新了调用
.to("netty4-http:https://localhost/dpm/idp/oauth/token?ssl=true&sslContextParameters=#sslContextParameters");
ssl = true 参数是必需的,我还为SSLContextParameters 配置了 bean,如下所示:
@Bean(name = "sslContextParameters")
public static SSLContextParameters sslParameters() throws KeyManagementException, GeneralSecurityException, IOException {
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("C:/myfolder/test.jks");
KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("jskPassword");
SSLContextParameters scp = new SSLContextParameters();
scp.setKeyManagers(kmp);
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLContext sslcontext = builder.build();
scp.createSSLContext().setDefault(sslcontext);
return scp;
}
我正在与已弃用的类作斗争。为了测试,我只弃用了一种方法,因为我应该使用继承。
如果我理解正确,我必须从我的自签名证书(.crt 和 .key 文件)开始为信任区生成一个JKS 文件。完成后,我添加了KeyStoreParameters 的说明和密码。
几乎解决了,但现在执行时出现此错误
PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 找到请求目标的有效认证路径
【问题讨论】:
标签: ssl https apache-camel netty