27

I am using Spring 5 WebClient. I want to know if it is possible to configure it to use an HTTP Proxy, or if there is a way of changing it's default configuration to do so.

Taylor Gautier
  • 4,916
  • 7
  • 30
  • 24

3 Answers3

40

This is something that the underlying client library should support.

When using Reactor Netty, you can do something like:

HttpClient httpClient = HttpClient.create()
            .tcpConfiguration(tcpClient ->
                    tcpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host("myproxyhost")));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
WebClient client = WebClient.builder().clientConnector(connector).build();
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Any examples of how to do this with a WebTestClient? – anschoewe Mar 20 '18 at 15:42
  • I guess WebTestClient is meant to test your app in a mock/integration setup and not an external service deployed behind a proxy. – Brian Clozel Mar 20 '18 at 15:43
  • 1
    Yeah... i was being lazy. Here's an example... ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options -> options.httpProxy(addressSpec -> { return addressSpec.host(proxyHost).port(proxyPort); })); WebTestClient client = WebTestClient .bindToServer(connector).baseUrl(hostname) .build(); – anschoewe Mar 20 '18 at 15:46
  • 6
    Thank you for your answer. before I tried to set the JVM flags (-DproxySet = true -DproxyHost = 127.0.0.1 -DproxyPort = 9999) before runing my application but it did not work. Do you have any idea why it doesn't works in this way with the WebClient? – Zrom May 24 '19 at 13:56
  • 2
    One addition: after ```host("myproxyhost")``` if you have port number don`t forget to put it. Otherwise it does not work. ```host("myproxyhost").port(portnumber)```. – Abdusoli Jul 07 '20 at 05:02
  • @BrianClozel, Good Day! I am new to reactive programming. Can you please share an example project Github link where the proxy is used with WebClient? Thank you in advance. – Rakib Hasan May 12 '22 at 03:50
6

" tcpConfiguration" is deprecated. So used this part of code instead.

  HttpClient httpClient =
            HttpClient.create()
                    .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
                            .host(sasConfig.getProxyHost())
                            .port(Integer.parseInt(sasConfig.getProxyPort())));

    ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

    WebClient webClient =  WebClient.builder().clientConnector(connector).build();
Reza
  • 1,906
  • 1
  • 17
  • 21
  • 3
    I have used the same but it throws an exception ```failure when writing TLS control frames; nested exception is javax.net.ssl.SSLException: failure when writing TLS control frames``` How do I solve this? – Utsav Sinha Jul 01 '21 at 07:50
  • 1
    This worked for me. :thumbs_up: – Hamid Nov 18 '21 at 20:54
1

Sharing recent experience here

Step 1 : Define proxy environment variables

-Dhttp.proxyHost=<proxyHost>
-Dhttp.proxyPort=8080
-Dhttps.proxyHost=<proxyHost>
-Dhttps.proxyPort=8080
-Dhttps.nonProxyHosts=localhost
  1. Configuration of proxy on webClient

    @Configuration
    public class WebClientConfiguration {    
     @Bean
     public WebClient webClient() {
         return WebClient.builder() //
                 .defaultHeader(ACCEPT, APPLICATION_JSON_VALUE) //
                 .clientConnector(new ReactorClientHttpConnector(httpClient())) //
                 .build();
     }
    
     private HttpClient httpClient() {
         return HttpClient //
                 .create() //
                 .proxyWithSystemProperties();
     }
    

    }

  2. Set the spring cloud proxy properties (In the application start)

    static {
       String nonProxyHosts = System.getProperty("http.nonProxyHosts");
       if (nonProxyHosts != null) {
         String regexProxyList = nonProxyHosts.replaceAll("\\.", "\\\\.").replaceAll("\\/", "\\\\/").replaceAll("\\*", ".\\*");
         System.setProperty("spring.cloud.gateway.httpclient.proxy.non-proxy-hosts-pattern", regexProxyList);
     }
     String proxyHost = System.getProperty("https.proxyHost");
     String proxyPort = System.getProperty("https.proxyPort");
     if (proxyHost != null && proxyPort != null) {
         System.setProperty("spring.cloud.gateway.httpclient.proxy.host", proxyHost);
         System.setProperty("spring.cloud.gateway.httpclient.proxy.port", proxyPort);
     }
    }
    
jfk
  • 4,335
  • 34
  • 27