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

7 java ssl restlet

我正在尝试使用Restlet的ClientResource将HTTPS连接到使用自签名证书的服务器.我使用一个独立的应用程序,只使用ClientResource,我的自定义SSLContextFactory添加为属性,代码可以在这里看到:

https://github.com/pixelatedpete/selfsignedexample

当我在一个更复杂的Restlet应用程序(使用与上面相同的pom)中使用相同的类(DynamicTrustManager和SelfSignSslSocketFactory)时,使用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);
Run Code Online (Sandbox Code Playgroud)

事情再次奏效.

这是否是其他任何人遇到过的事情,并且可以指出我怀疑是一件非常明显的事我错过了什么?

铌.使用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
Run Code Online (Sandbox Code Playgroud)

还尝试注入SslContextFactory(我们在这里使用Guice),但这也没有帮助.

小智 3

好吧,终于弄清楚了 - 我错过了客户端位:

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