You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.2 RestClient如何设置带认证的代理访问外部IP?

Spring Boot 3.2 RestClient 代理配置(含认证)

1. 基础无认证代理配置

如果代理无需用户名密码认证,直接通过SimpleClientHttpRequestFactory配置代理地址和端口即可:

import org.springframework.web.client.RestClient;
import java.net.InetSocketAddress;
import java.net.Proxy;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

public class RestClientProxyExample {
    public static void main(String[] args) {
        // 配置代理地址和端口
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080));

        RestClient restClient = RestClient.builder()
                .requestFactory(clientHttpRequestFactory -> {
                    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
                    factory.setProxy(proxy);
                    return factory;
                })
                .build();

        // 测试请求
        String response = restClient.get()
                .uri("https://external-api.example.com/data")
                .retrieve()
                .body(String.class);

        System.out.println(response);
    }
}

2. 带代理认证的配置

当代理需要用户名密码认证时,结合Authenticator设置认证信息:

import org.springframework.web.client.RestClient;
import java.net.*;
import java.util.Properties;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

public class RestClientProxyAuthExample {
    public static void main(String[] args) {
        // 代理信息
        String proxyHost = "proxy.example.com";
        int proxyPort = 8080;
        String proxyUser = "your-proxy-username";
        String proxyPassword = "your-proxy-password";

        // 设置代理认证逻辑
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (getRequestingHost().equalsIgnoreCase(proxyHost)) {
                    return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
                }
                return super.getPasswordAuthentication();
            }
        });

        // 配置系统属性(兼容底层客户端代理识别)
        Properties systemProps = System.getProperties();
        systemProps.setProperty("http.proxyHost", proxyHost);
        systemProps.setProperty("http.proxyPort", String.valueOf(proxyPort));
        systemProps.setProperty("https.proxyHost", proxyHost);
        systemProps.setProperty("https.proxyPort", String.valueOf(proxyPort));

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

        RestClient restClient = RestClient.builder()
                .requestFactory(clientHttpRequestFactory -> {
                    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
                    factory.setProxy(proxy);
                    return factory;
                })
                .build();

        // 发起带代理认证的请求
        String response = restClient.get()
                .uri("https://external-api.example.com/protected-data")
                .retrieve()
                .body(String.class);

        System.out.println(response);
    }
}

3. Spring Boot 配置类方式(推荐复用)

在Spring Boot项目中,通过配置类注入RestClient Bean,便于全局复用:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import java.net.*;
import java.util.Properties;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

@Configuration
public class RestClientProxyConfig {

    @Bean
    public RestClient restClient() {
        // 可从application.yml/properties读取配置,替换硬编码值
        String proxyHost = "proxy.example.com";
        int proxyPort = 8080;
        String proxyUser = "your-proxy-username";
        String proxyPassword = "your-proxy-password";

        // 设置代理认证
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (getRequestingHost().equalsIgnoreCase(proxyHost)) {
                    return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
                }
                return super.getPasswordAuthentication();
            }
        });

        // 配置系统属性
        Properties systemProps = System.getProperties();
        systemProps.setProperty("http.proxyHost", proxyHost);
        systemProps.setProperty("http.proxyPort", String.valueOf(proxyPort));
        systemProps.setProperty("https.proxyHost", proxyHost);
        systemProps.setProperty("https.proxyPort", String.valueOf(proxyPort));

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

        return RestClient.builder()
                .requestFactory(factory -> {
                    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
                    requestFactory.setProxy(proxy);
                    return requestFactory;
                })
                .build();
    }
}

关键说明

  • SimpleClientHttpRequestFactory是Spring默认请求工厂,支持直接绑定Proxy对象
  • Authenticator通过匹配代理主机返回认证信息,避免全局认证冲突
  • 系统属性配置为兼容部分底层HTTP客户端的代理识别逻辑,可根据实际场景调整

内容的提问来源于stack exchange,提问作者ROBLCSnail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:13:27