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;
    }
}
相关推荐
IT_陈寒2 分钟前
React性能优化:这5个Hook技巧让我的组件渲染效率提升50%(附代码对比)
前端·人工智能·后端
Captaincc4 分钟前
9 月 20 日,TRAE Meetup@Guangzhou 相聚羊城
人工智能·后端
Brookty7 分钟前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
风象南23 分钟前
SpringBoot Jar包冲突在线检测
后端
tellmewhoisi25 分钟前
前置配置1:nacos 基本配置(注册与发现)
java
程序员爱钓鱼26 分钟前
Go语言实战案例 — 项目实战篇:任务待办清单 Web 应用
后端·google·go
会开花的二叉树30 分钟前
继承与组合:C++面向对象的核心
java·开发语言·c++
长河2 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
Cyan_RA93 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
喵手4 小时前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络