带有NettyConnectorProvider的JerseyClient如何使用HTTPS代理

时间:2018-08-08 09:16:11

标签: java https jersey fiddler

  1. 现在,我可以使用Fiddler通过HttpUrlConnectionProvider从JerseyClient捕获HTTPS通信。
  2. 我将Fiddler的证书导入到我的TrustStore,其中包含“ jre \ lib \ security \ cacerts”中的所有证书。确实有效
  3. 但是当我将JerseyClient与NettyConnectorProvider一起使用时,它不起作用。

  4. 我想知道问题出在哪里?我调试了源代码证书正确,为什么TSL握手无法建立? NettyConnector Source Code

    这是正确的:

    Properties props = System.getProperties();
    props.setProperty("https.proxyHost", "localhost");
    props.setProperty("https.proxyPort", "8888");
    System.setProperties(props);
    
    String trustStorePath = Thread.currentThread().getContextClassLoader().getResource("mytruststore").getPath();
    System.setProperty("javax.net.ssl.trustStore",  trustStorePath);
    System.setProperty("javax.net.ssl.trustStorePassword",  "changeit");
    
    HttpURLConnection http = (HttpURLConnection) new URL("https://www.baidu.com?name=123").openConnection();
    InputStream in = http.getInputStream();
    
    BufferedReader bf = new BufferedReader(new InputStreamReader(in));
    String str = null;
    while ( (str = bf.readLine()) != null) {
        System.out.println(str);
    }
    

    这不起作用:

    String path = Thread.currentThread().getContextClassLoader().getResource("mytruststore").getPath();
    
    System.out.println(path);
    SslConfigurator sslConfig = SslConfigurator.newInstance()
            .trustStoreFile(path)
            .trustStorePassword("changeit");
    
    SSLContext sslContext = sslConfig.createSSLContext();
    
    ClientConfig config = new ClientConfig();
    config.property(ClientProperties.PROXY_URI, "http://localhost:8888");
    config.property(ClientProperties.PROXY_USERNAME, "");
    config.property(ClientProperties.PROXY_PASSWORD, "");
    config.connectorProvider(new NettyConnectorProvider());
    
    Client client = ClientBuilder.newBuilder().withConfig(config).sslContext(sslContext).build();
    
    WebTarget webTarget = client.target("https://www.baidu.com");
    String res = webTarget.request().get(String.class);
    System.out.println(res);
    

我有一个例外:

 Caused by: io.netty.handler.proxy.ProxyConnectException: http, basic, localhost/127.0.0.1:8888 => www.baidu.com/:443, java.nio.channels.ClosedChannelException
at io.netty.handler.proxy.ProxyHandler.setConnectFailure(ProxyHandler.java:344)
at io.netty.handler.proxy.ProxyHandler.access$100(ProxyHandler.java:38)
at io.netty.handler.proxy.ProxyHandler$1.operationComplete(ProxyHandler.java:67)
at io.netty.handler.proxy.ProxyHandler$1.operationComplete(ProxyHandler.java:63)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:481)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:420)
at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:122)
at io.netty.channel.PendingWriteQueue.safeFail(PendingWriteQueue.java:298)
at io.netty.channel.PendingWriteQueue.removeAndFailAll(PendingWriteQueue.java:189)
at io.netty.handler.ssl.SslHandler.setHandshakeFailure(SslHandler.java:1409)
at io.netty.handler.ssl.SslHandler.channelInactive(SslHandler.java:895)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1329)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:908)
at io.netty.channel.AbstractChannel$AbstractUnsafe$7.run(AbstractChannel.java:744)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案