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

devtools/2024/10/18 23:25:10/

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


http://www.ppmy.cn/devtools/95143.html

相关文章

一文彻底搞懂Transformer - FFNN(前馈神经网络)

Transformer __一、神经网络&#xff08;Neural Network&#xff09;__ 神经网络&#xff1a; 神经网络&#xff08;Neural Networks&#xff09;是一种模仿生物神经网络的结构和功能的数学或计算模型。它由大量的人工神经元&#xff08;也称为节点或处理单元&#xff09;相互…

C++11标准模板(STL)- 算法库 - 类似 std::accumulate,但不依序执行 -(std::reduce)

算法库 算法库提供大量用途的函数&#xff08;例如查找、排序、计数、操作&#xff09;&#xff0c;它们在元素范围上操作。注意范围定义为 [first, last) &#xff0c;其中 last 指代要查询或修改的最后元素的后一个元素。 类似 std::accumulate&#xff0c;但不依序执行 std…

后端Web之分层解耦(控制反转IOC-依赖注入DI)

目录 1.三层架构 2.IOC-DI引入 3.IOC-DI使用 4.IOC细节 5.DI细节 内聚&#xff08;Cohesion&#xff09;和耦合&#xff08;Coupling&#xff09;是软件工程中两个重要的概念&#xff0c;它们衡量了软件组件的组织方式和组件之间的相互依赖程度。高内聚性意味着模块内的元…

OSI 七层网络模型和 TCP/IP 四层网络模型

OSI 七层网络模型 网络的七层架构从下到上主要分为&#xff1a;物理层、数据链路层、网络层、传输层、会话层、表示层和应用层 物理层主要定义物理设备标准&#xff0c;它的主要作用是传输比特流&#xff0c;具体做法是在发送端将 1、0 码转化为电流强弱来进行传输&#xff0…

Linux内核编程(十二)热插拔

本文目录 一、知识点1. 热插拔概念2. 热插拔机制3. Netlink机制 二、内核发送uevent事件到用户空间1. kobject发送uevent事件2. udevadm命令查看★示例代码&#xff1a;★优化&#xff1a;完善kset_uevent_ops&#xff08;热插拔事件结构体&#xff09; 三、用户空间使用Netlin…

TypeError: Cannot read properties of undefined (reading ‘scrollIntoView‘)(已解决)

问题复现&#xff1a;眨眼睛使用vitevue3实现跳转dom功能时使用了scrollIntoView方法&#xff0c;在打包上传以后使用该功能报错 小友可能会陷入误区&#xff0c;以为是函数方法有问题&#xff0c;毕竟在开发时是没有问题的&#xff0c; 而实际上呢问题出在获取节点失败了 在这…

使用 Spring Boot 实现职责链模式处理电商订单流程

业务场景 假设我们正在构建一个电商系统&#xff0c;需要处理用户的订单请求。当用户提交订单时&#xff0c;系统需要执行一系列检查和操作&#xff0c;比如验证库存、检查用户信用额度、确认支付方式等。如果所有检查都通过&#xff0c;则订单被创建&#xff1b;否则&#xf…