You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何配置Spring Cloud Feign通过带认证的企业代理调用外部REST服务?

配置Spring Cloud Feign使用带身份认证的企业代理

我明白你找这个解决方案找了挺久,刚好我之前处理过类似的场景,给你几个完全适配Spring Boot自动构建Feign实例的可行方案,不用手动写Feign.builder()那套代码:

前提准备

首先确保你的项目已经引入了Spring Cloud Feign的依赖:

<!-- Maven依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

我们可以选择Apache HttpClient或者OkHttp作为Feign的底层客户端来配置代理,以下分别介绍两种方案:


方案一:针对单个Feign Client配置代理

如果你只需要特定的Feign Client走代理,可以创建专属的配置类:

1. 引入Apache HttpClient依赖(可选,也可以用OkHttp)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-httpclient</artifactId>
</dependency>

2. 创建代理配置类

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignProxyConfig {

    @Bean
    public CloseableHttpClient httpClient() {
        // 替换为你的企业代理信息
        String proxyHost = "your-company-proxy-host";
        int proxyPort = 8080;
        String proxyUsername = "your-proxy-username";
        String proxyPassword = "your-proxy-password";

        // 配置代理身份认证
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(proxyUsername, proxyPassword)
        );

        // 构建带代理的HttpClient
        return HttpClients.custom()
                .setProxy(new org.apache.http.HttpHost(proxyHost, proxyPort))
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();
    }
}

3. 在Feign Client上指定配置类

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(
        name = "external-rest-service",
        url = "https://your-external-api.com",
        configuration = FeignProxyConfig.class // 指定刚才的代理配置类
)
public interface ExternalServiceFeignClient {

    @GetMapping("/api/v1/data")
    String fetchExternalData();
}

方案二:全局配置所有Feign Client使用代理

如果所有外部调用都需要走企业代理,可以配置全局生效的规则:

1. 配置文件中定义代理参数

application.yml(或application.properties)中添加代理配置:

# 代理配置
proxy:
  host: your-company-proxy-host
  port: 8080
  username: your-proxy-username
  password: your-proxy-password

# 启用Apache HttpClient作为Feign客户端
feign:
  httpclient:
    enabled: true

2. 创建全局Feign代理配置类

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GlobalFeignProxyConfig {

    @Value("${proxy.host}")
    private String proxyHost;

    @Value("${proxy.port}")
    private int proxyPort;

    @Value("${proxy.username}")
    private String proxyUsername;

    @Value("${proxy.password}")
    private String proxyPassword;

    @Bean
    public CloseableHttpClient httpClient() {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(proxyUsername, proxyPassword)
        );

        return HttpClients.custom()
                .setProxy(new org.apache.http.HttpHost(proxyHost, proxyPort))
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();
    }
}

3. 启动类绑定全局配置

在Spring Boot启动类上,通过@EnableFeignClients指定全局默认配置:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients(defaultConfiguration = GlobalFeignProxyConfig.class)
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

可选:用OkHttp作为Feign客户端配置代理

如果你偏好使用OkHttp,步骤类似:

1. 引入OkHttp依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
</dependency>

2. 配置文件启用OkHttp

feign:
  okhttp:
    enabled: true

3. 创建OkHttp代理配置类

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Proxy;
import okhttp3.Route;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class FeignOkHttpProxyConfig {

    @Value("${proxy.host}")
    private String proxyHost;

    @Value("${proxy.port}")
    private int proxyPort;

    @Value("${proxy.username}")
    private String proxyUsername;

    @Value("${proxy.password}")
    private String proxyPassword;

    @Bean
    public OkHttpClient okHttpClient() {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new java.net.InetSocketAddress(proxyHost, proxyPort));

        // 代理身份认证逻辑
        Authenticator proxyAuthenticator = new Authenticator() {
            @Override
            public okhttp3.Request authenticate(Route route, okhttp3.Response response) throws IOException {
                String credential = Credentials.basic(proxyUsername, proxyPassword);
                return response.request().newBuilder()
                        .header("Proxy-Authorization", credential)
                        .build();
            }
        };

        return new OkHttpClient.Builder()
                .proxy(proxy)
                .proxyAuthenticator(proxyAuthenticator)
                .build();
    }
}

之后同样可以选择单个Feign Client指定该配置,或者全局绑定。


注意事项

  • 代理密码不要明文存储在配置文件中,建议使用Spring Cloud Config配合加密,或者Jasypt对敏感配置进行加密。
  • 如果企业代理需要HTTPS证书验证,可能需要额外配置SSL信任管理器,具体可以根据代理的SSL要求调整HttpClient/OkHttp的构建逻辑。

内容的提问来源于stack exchange,提问作者Diego Cândido da Silva

火山引擎 最新活动