Springboot如何实现代理服务器
作者:了佳世客
这篇文章主要介绍了Springboot如何实现代理服务器问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Springboot实现代理服务器
Vue使用axios请求出现跨域问题,需要代理服务器。
代理服务器是介于浏览器和Web服务器之间的一台服务器,有了它之后,浏览器不是直接到Web服务器去取回网页而是向代理服务器发出请求,请求信号会先送到代理服务器,由代理服务器来取回浏览器所需要的信息并传送给你的浏览器。
代理服务器不只是简单地向服务器转发请求,它还可以控制用户的行为,对接收到的客户请求进行决策,并根据过滤规则对用户请求进行过滤
我使用spring boot,想快速的实现一个用于测试的代理服务器,因此使用了HTTP-Proxy-Servlet
集成步骤:
导入依赖
<dependency> <groupId>org.mitre.dsmiley.httpproxy</groupId> <artifactId>smiley-http-proxy-servlet</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency>
创建bean
@Bean
public Servlet baiduProxyServlet(){
return new ProxyServlet();
}
@Bean
public ServletRegistrationBean proxyServletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(baiduProxyServlet(), "/*");
Map<String, String> params = ImmutableMap.of(
"targetUri", "http://www.baidu.com",//目标服务器
"log", "true");
registrationBean.setInitParameters(params);
return registrationBean;
}设置允许跨域请求
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}设置代理服务器端口
server.port=8801
启动项目,访问http://localhost:8801/,就会访问到http://www.baidu.com
Springboot代理设置
配置文件
my: proxy: http: addr: 3.3.3.3 port: 1111 https: addr: 3.3.3.3 port: 1112 exclude: paths: - 1.1.1.1 - 2.2.2.2
加载过滤地址
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Data
@Configuration
@ConfigurationProperties(prefix = "my.proxy.exclude")
public class ProxyConfig {
List<String> paths;
}设置代理
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import pers.zzq.manage.config.ProxyConfig;
import java.util.List;
@Component
@Slf4j
@EnableConfigurationProperties(ProxyConfig.class)
public class ProxyInteceptorRunner implements CommandLineRunner {
@Autowired
ProxyConfig proxyConfig;
@Value("${my.proxy.http.addr}")
String httpAddr;
@Value("${my.proxy.http.port}")
String httpPort;
@Value("${my.proxy.https.addr}")
String httpsAddr;
@Value("${my.proxy.https.port}")
String httpsPort;
@Override
public void run(String... args) {
System.setProperty("https.proxyHost", httpsAddr);
System.setProperty("https.proxyPort", httpsPort);
System.setProperty("http.proxyHost", httpAddr);
System.setProperty("http.proxyPort", httpPort);
StringBuilder sb = new StringBuilder();
List<String> paths = proxyConfig.getPaths();
for (String path : paths) {
sb.append(path).append("|");
}
String s = sb.toString() + "localhost";
System.setProperty("http.nonProxyHosts", s);
System.setProperty("http.nonProxyHosts", s);
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
