如何在Jetty上运行的Restlet应用程序中使用自定义SSLContextFactory?

7
我正在尝试使用Restlet的ClientResource连接到使用自签名证书的服务器的HTTPS。我已经通过一个独立的应用程序,只使用ClientResource和我的自定义SSLContextFactory添加为属性,并且可以在此处查看其代码,使其正常工作:

https://github.com/pixelatedpete/selfsignedexample

当我在更复杂的Restlet应用程序中(具有与上述相同的pom),它使用Restlet提供通过Jetty提供REST API时,我的自定义SSLContextFactory不再被使用。

我像上面一样将其添加到ClientResource上下文中,但我从未看到任何日志消息表明提供给ClientResource的SSLContextFactory被传递到底层httpclient。

如果我改用直接使用HttpClient而不是ClientResource:

HttpPost post = new HttpPost(cr.getReference().toString());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(...);
DynamicTrustManager tm = new DynamicTrustManager(..., cert);
SelfSignTrustSslContextFactory scf = (SelfSignTrustSslContextFactory) 
CloseableHttpClient httpclient = HttpClients.custom().setSslcontext(scf.createSslContext()).build();
CloseableHttpResponse response = httpclient.execute(post);

事情又开始运作了。

有没有其他人遇到过这种情况,并能指出我所怀疑的非常明显但我却忽略的东西?

注意:尝试使用Tomcat再次,仍然存在同样的问题。

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我们还尝试过注入SslContextFactory(这里使用的是Guice),但这也没有帮助。

1个回答

3

好的,最终弄清楚了 - 我漏掉了客户端部分:

Client client = new Client(crCtx, Protocol.HTTPS);
ClientResource clientResource = new ClientResource("https://example.com");
clientResource.setNext(client);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接