【使用ProxyServlet代理多个地址】

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.boring</groupId>
    <artifactId>net_agent</artifactId>
    <version>0.0.1</version>
    <name>net_agent</name>
    <description>net_agent</description>
    <packaging>war</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mitre.dsmiley.httpproxy</groupId>
            <artifactId>smiley-http-proxy-servlet</artifactId>
            <version>1.12.1</version>
        </dependency>
    </dependencies>

</project>

application.yml配置

#端口随便写
server:
  port: 8761
#地址映射,可配多个
proxy:
  url_mapping:
    {
    #访问http://localhost:8761/test1就会打开https://www.csdn.net
      "[/test1/*]": "https://www.csdn.net",
      "[/test2/*]": "https://www.bilibili.com",
      "[/test1/sp/*]": "https://spring.io"
    }

配置类

package com.boring.net_agent.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Map;

/**
 * <h3>net_agent</h3>
 * <p>代理地址映射配置</p>
 *
 * @author : AbstractBin
 * @date : 2022/3/10 0:11
 **/
@Configuration
@ConfigurationProperties(prefix = "proxy")
@PropertySource("classpath:application.yml")
public class ProxyMappingConfig {
    //将yml中的proxy.url_mapping自动转为map
    private Map<String, String> urlMapping;
	
    public void setUrlMapping(Map<String, String> urlMapping) {
        this.urlMapping = urlMapping;
    }
    public Map<String, String> getUrlMapping() {
        return urlMapping;
    }
}

启动时自动添加servlet

package com.boring.net_agent.filter;

import com.boring.net_agent.config.ProxyMappingConfig;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <h3>net_agent</h3>
 * <p></p>
 *
 * @author : AbstractBin
 * @date : 2022/3/10 9:35
 **/
@Component
public class ServletContextInitListener implements ServletContextInitializer {

    @Autowired
    private ProxyMappingConfig proxyMappingConfig;

    //项目启动时,会读取yml配置里所有的路径映射,每个映射注册一个ProxyServlet
    @Override
    public void onStartup(ServletContext servletContext) {
        Map<String, String> urlMapping = proxyMappingConfig.getUrlMapping();
        if (null == urlMapping || urlMapping.size() < 1) return;
        //计数
        AtomicInteger count = new AtomicInteger(0);
        urlMapping.forEach((serverUrl, targetUrl) -> {
            ProxyServlet servlet = new ProxyServlet();
            //每个servlet名字不能重复
            String servletName = "servlet" + count.get();
            ServletRegistration.Dynamic dynamic = servletContext.addServlet(servletName, servlet);
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put(ProxyServlet.P_TARGET_URI, targetUrl);
            paramMap.put("log", "true");
            dynamic.setInitParameters(paramMap);
            dynamic.addMapping(serverUrl);
            count.incrementAndGet();
        });
    }
}

快去试试吧

启动项目,访问http://localhost:8761/test1就会打开https://www.csdn.net

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值