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.
Asked
Active
Viewed 1.6k times
3 Answers
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
-
1Yeah... 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
-
6Thank 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
-
2One 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
-
3I 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
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
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(); }
}
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