思路是创建一个servlet,解析路径映射,在其中实现请求消息体,消息头的转发。响应消息体消息头的转发。
实现工具https://mvnrepository.com/artifact/org.mitre.dsmiley.httpproxy/smiley-http-proxy-servlet/1.12.1
<dependency><groupId>org.mitre.dsmiley.httpproxy</groupId><artifactId>smiley-http-proxy-servlet</artifactId><version>1.12.1</version>
</dependency>
yml:
# 自定义代理相关配置
# 代理的本地路由
proxy.servlet_url: /arcgisProxy/*
# 要代理的地址
proxt.target_url: http://www.baidu.com
配置类:
@Configuration
public class ProxyConfig {// 读取配置文件中路由设置@Value("${proxy.servlet_url}")private String servlet_url;// 读取配置中代理目标地址@Value("${proxt.target_url}")private String target_url;@Beanpublic Servlet createProxyServlet(){// 创建新的ProxyServletreturn new ProxyServlet();}@Beanpublic ServletRegistrationBean proxyServletRegistration(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(createProxyServlet(), servlet_url);//设置网址以及参数Map<String, String> params = ImmutableMap.of(ProxyServlet.P_TARGET_URI, target_url,ProxyServlet.P_CONNECTTIMEOUT, "20000",ProxyServlet.P_READTIMEOUT, "20000",ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000","log", "false");registrationBean.setInitParameters(params);return registrationBean;}
}
多个代理:
package com.municipal.promotion.second_phase.config;import com.google.common.collect.ImmutableMap;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.servlet.Servlet;
import java.util.Map;/*** @author yaoct* @date 2023/11/13 10:31* @description:*/
@Configuration
public class ProxyConfig {// 读取配置文件中路由设置@Value("${proxy.servlet_url}")private String servlet_url;// 读取配置中代理目标地址@Value("${proxt.target_url}")private String target_url;// 读取配置文件中路由设置@Value("${proxy.servlet_url2}")private String servlet_url2;// 读取配置中代理目标地址@Value("${proxt.target_url2}")private String target_url2;@Beanpublic Servlet createProxyServlet(){// 创建新的ProxyServletreturn new ProxyServlet();}@Beanpublic ServletRegistrationBean proxyServletRegistration(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servlet_url);registrationBean.setName("proxyServlet1");//名字需要不一样!//设置网址以及参数Map<String, String> params = ImmutableMap.of(ProxyServlet.P_TARGET_URI, target_url,ProxyServlet.P_CONNECTTIMEOUT, "20000",ProxyServlet.P_READTIMEOUT, "20000",ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000","log", "false");registrationBean.setInitParameters(params);return registrationBean;}// @Bean
// public Servlet createProxyServlet2(){
// // 创建新的ProxyServlet
// return new ProxyServlet();
// }@Beanpublic ServletRegistrationBean proxyServletRegistration2(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servlet_url2);registrationBean.setName("proxyServlet2");//名字需要不一样!//设置网址以及参数Map<String, String> params = ImmutableMap.of(ProxyServlet.P_TARGET_URI, target_url2,ProxyServlet.P_CONNECTTIMEOUT, "20000",ProxyServlet.P_READTIMEOUT, "20000",ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000","log", "false");registrationBean.setInitParameters(params);return registrationBean;}
}