Springboot如何實(shí)現(xiàn)代理服務(wù)器
Springboot實(shí)現(xiàn)代理服務(wù)器
Vue使用axios請(qǐng)求出現(xiàn)跨域問題,需要代理服務(wù)器。
代理服務(wù)器是介于瀏覽器和Web服務(wù)器之間的一臺(tái)服務(wù)器,有了它之后,瀏覽器不是直接到Web服務(wù)器去取回網(wǎng)頁而是向代理服務(wù)器發(fā)出請(qǐng)求,請(qǐng)求信號(hào)會(huì)先送到代理服務(wù)器,由代理服務(wù)器來取回瀏覽器所需要的信息并傳送給你的瀏覽器。
代理服務(wù)器不只是簡單地向服務(wù)器轉(zhuǎn)發(fā)請(qǐng)求,它還可以控制用戶的行為,對(duì)接收到的客戶請(qǐng)求進(jìn)行決策,并根據(jù)過濾規(guī)則對(duì)用戶請(qǐng)求進(jìn)行過濾
我使用spring boot,想快速的實(shí)現(xiàn)一個(gè)用于測試的代理服務(wù)器,因此使用了HTTP-Proxy-Servlet
集成步驟:
導(dǎo)入依賴
<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>
創(chuàng)建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",//目標(biāo)服務(wù)器
? ? ? ? ? ? ? ? "log", "true");
? ? ? ? registrationBean.setInitParameters(params);
? ? ? ? return registrationBean;
? ? }設(shè)置允許跨域請(qǐng)求
@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("*");
? ? }
}設(shè)置代理服務(wù)器端口
server.port=8801
啟動(dòng)項(xiàng)目,訪問http://localhost:8801/,就會(huì)訪問到http://www.baidu.com
Springboot代理設(shè)置
配置文件
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;
}設(shè)置代理
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);
? ?}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java模擬棧的實(shí)現(xiàn)以及Stack類的介紹
棧是一種數(shù)據(jù)結(jié)構(gòu),它按照后進(jìn)先出的原則來存儲(chǔ)和訪問數(shù)據(jù)。Stack是一個(gè)類,表示棧數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)。本文就來和大家介紹一下Java模擬棧的實(shí)現(xiàn)以及Stack類的使用,需要的可以參考一下2023-04-04
java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)項(xiàng)目
這篇文章主要介紹了java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Linux部署springboot項(xiàng)目彩色日志打印方式
這篇文章主要介紹了Linux部署springboot項(xiàng)目彩色日志打印方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Java Base64算法實(shí)際應(yīng)用之郵件發(fā)送實(shí)例分析
這篇文章主要介紹了Java Base64算法實(shí)際應(yīng)用之郵件發(fā)送,結(jié)合實(shí)例形式分析了java字符編碼與郵件發(fā)送相關(guān)操作技巧,需要的朋友可以參考下2019-09-09

