如何使用Spring 5 WebClient支持HTTP代理?

27

我正在使用Spring 5 WebClient,想知道是否可以配置它使用HTTP代理,或者改变默认配置以这样做。

3个回答

40

这是底层客户端库应该支持的内容。

当使用 Reactor Netty 时,你可以这样做:

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();

如何使用WebTestClient完成此操作的任何示例? - anschoewe
我猜WebTestClient是用来在模拟/集成环境中测试您的应用程序,而不是在代理后部署的外部服务。 - Brian Clozel
1
是的...我有点懒。这里有一个例子...ReactorClientHttpConnector连接器 = new ReactorClientHttpConnector(options -> options.httpProxy(addressSpec -> { return addressSpec.host(proxyHost).port(proxyPort); }));WebTestClient客户端 = WebTestClient .bindToServer(connector).baseUrl(hostname) .build(); - anschoewe
6
谢谢您的回复。在运行应用程序之前,我尝试设置JVM标志(-DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=9999),但它没有起作用。您有任何想法为什么WebClient不能以这种方式工作吗? - Zrom
2
一个补充:在 host("myproxyhost") 后,如果你有端口号,请不要忘记输入它。否则它将无法工作。host("myproxyhost").port(portnumber) - Abdusoli
@BrianClozel, 您好! 我是响应式编程的新手。您能否分享一个使用WebClient代理的示例项目Github链接? 提前感谢您。 - Rakib Hasan

6
"

"tcpConfiguration"已被废弃。请使用以下代码片段。

"
  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();

3
我使用了相同的方法,但它报错了: 在写入TLS控制帧时失败;嵌套异常是javax.net.ssl.SSLException:在写入TLS控制帧时失败 我该如何解决? - Utsav Sinha
1
这对我有用。� - Hamid

1
分享最近的经验
步骤1:定义代理环境变量。
-Dhttp.proxyHost=<proxyHost>
-Dhttp.proxyPort=8080
-Dhttps.proxyHost=<proxyHost>
-Dhttps.proxyPort=8080
-Dhttps.nonProxyHosts=localhost
  • 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();
     }
    

    }

  • 设置Spring Cloud代理属性(在应用程序启动时)

    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);
     }
    }
    

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