使用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