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;



    @Bean
    public Servlet createProxyServlet(){
        // 创建新的ProxyServlet
        return new ProxyServlet();
    }
    @Bean
    public 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;



    @Bean
    public Servlet createProxyServlet(){
        // 创建新的ProxyServlet
        return new ProxyServlet();
    }
    @Bean
    public 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();
//    }
    @Bean
    public 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;
    }
}
相关推荐
.格子衫.8 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多8 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
摇滚侠10 小时前
Spring Boot 3零基础教程,Spring Intializer,笔记05
spring boot·笔记·spring
Jabes.yang11 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪11 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人11 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR2211 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠11 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
华洛11 小时前
公开一个AI产品的商业逻辑与设计方案——AI带来的涂色卡自由
前端·后端·产品
追逐时光者11 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 57 期(2025年10.1-10.12)
后端·.net