一、背景
使用场景大总结,可作为参考手册用
二、使用场景
1、所有内网应用都不能直接访问外网,但需要能通过(内部的)外网代理服务器访问
2、自己不能上网,蹭别人的网,局域网代理软件Ccproxy也是一种正向代理
3、大家访问外国网站使用的梯子(翻墙)也是一种正向代理
三、环境
- Nginx 1.27.13 官方下载地址:Nginx 1.27.3
- ngx_http_proxy_connect_module 1.27.1 官方下载地址:ngx_http_proxy_connect_module
四、接口(级)使用代理
1、http接口使用代理
-》openfeign
说明:
应用级, 同时也可以通过proxySelector作为接口级来用
第一步:配置文件允许覆盖Bean
spring:
main:
allow-bean-definition-overriding: true
第二步:配置Bean
package person.brickman.config;
import okhttp3.*;
import org.springframework.cloud.commons.httpclient.DefaultOkHttpClientFactory;
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
@Configuration
//配置openfein接口的文件件路径
@EnableFeignClients(basePackages = "com.ciih.refine.server")
public class Config {
@Bean
public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) {
return new ProxyOkHttpClientFactory(builder);
}
static class ProxyOkHttpClientFactory extends DefaultOkHttpClientFactory {
public ProxyOkHttpClientFactory(OkHttpClient.Builder builder) {
super(builder);
//配置IP、端口
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.40.3.XXX ", 9999));
builder.proxy(proxy);
builder.proxyAuthenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
//设置代理服务器账号密码
String credential = Credentials.basic("admin", "admin");
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
});
//如果要配置限制域则加上下面
/*List<Proxy> proxyList = new ArrayList<>(1);
proxyList.add(proxy);
builder.proxySelector(new ProxySelector() {
//限制域
Set<String> domainList;
@Override
public List<Proxy> select(URI uri) {
i