[我使用Apache Camel公开了一个简单的REST服务,例如Spring boot microservice,它使用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错误的请求错误。从日志中我读取到请求以HTTP而不是HTTPS格式到达,但我不明白为什么:
您正在对支持SSL的服务器端口使用普通HTTP。改为使用请使用HTTPS方案访问此URL。
如果我使用邮递员调用服务https://localhost/service/test
,它将正常工作。
SSL配置有自签名证书。
如何使用Apache骆驼中的netty组件创建正确的https请求?该文档仅建议替换该协议,最多只能使用一些选项,但是这些选项不起作用。
UPDATE
我以这种方式更新了通话
.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 {
SSLContextParameters scp = new SSLContextParameters();
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLContext sslcontext = builder.build();
scp.createSSLContext().setDefault(sslcontext);
return scp;
}
我与不推荐使用的类进行了一些斗争。对于测试,我只保留一种不赞成使用的方法,因为我应该使用继承。它几乎解决了,但是现在我收到此错误
PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法查找到所请求目标的有效认证路径
如果我理解正确,那是因为我没有使用证书设置KeyStoreParameters
,但是现在我很困惑。
是自我生成的,我不应该像邮递员或浏览器那样跳过支票吗?我从其他文档中了解到,我应该修改HttpClient
配置以禁用hostname verifier,但是Netty
似乎没有提供执行此操作的选项。
同时,我试图将证书的路径指示给Netty
,只是为了重新排列想法。
我有点困惑,在这一点上,我认为我还应该根据资源使用的证书来更新代码(例如,如果需要密码进行检查)。
您可能需要configure a sslContextParameters
object才能用于SSL的sslContextParameters
。
我不确定参数名称。文档说configure the Netty component,但我认为是sslContextParameters
。
sslContextParametersRef
.to("netty4-http:https://localhost/service/test?sslContextParametersRef=#sslConfig");
意味着Camel可以从注册表中使用标识符#sslConfig
来获取对象。因此,例如对于Spring,它将是ID为sslConfig
的Spring托管Bean。
sslConfig
(不是http)也有参数Netty component。不知道ssl=true
是否也需要这样做。因此,您将不得不使用这些不同的参数进行测试。
通过Netty组件的文档具有Netty-http等的方式。看一下。