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 分钟前
Java回溯算法解决非递减子序列问题(LeetCode 491)的深度解析
java·算法·leetcode
众乐乐_20089 分钟前
Java 后端给前端传Long值,精度丢失的问题与解决
java·前端·状态模式
北辰浮光15 分钟前
[springboot]SSM日期数据转换易见问题
java·spring boot·后端
两点王爷15 分钟前
IDEA中springboot项目中连接docker
spring boot·docker·intellij-idea
木梓辛铭22 分钟前
Spring Cache的详细使用
java·后端·spring
招风的黑耳25 分钟前
Java视频流RTMP/RTSP协议解析与实战代码
java·视频流
邪恶的贝利亚40 分钟前
定时器设计
java·linux·前端
工业互联网专业41 分钟前
基于springboot+vue的机场乘客服务系统
java·vue.js·spring boot·毕业设计·源码·课程设计·机场乘客服务系统
饕餮争锋42 分钟前
WebMvcConfigurer介绍-笔记
java·笔记·servlet
招风的黑耳1 小时前
Java集合框架详解与使用场景示例
java·开发语言