您的位置:首页 > 文旅 > 美景 > springboot后端当成反向代理服务器

springboot后端当成反向代理服务器

2025/2/28 17:40:29 来源:https://blog.csdn.net/yaoct/article/details/141222941  浏览:    关键词:springboot后端当成反向代理服务器

思路是创建一个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;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com