Springboot如何實現(xiàn)代理服務(wù)器
Springboot實現(xiàn)代理服務(wù)器
Vue使用axios請求出現(xiàn)跨域問題,需要代理服務(wù)器。
代理服務(wù)器是介于瀏覽器和Web服務(wù)器之間的一臺服務(wù)器,有了它之后,瀏覽器不是直接到Web服務(wù)器去取回網(wǎng)頁而是向代理服務(wù)器發(fā)出請求,請求信號會先送到代理服務(wù)器,由代理服務(wù)器來取回瀏覽器所需要的信息并傳送給你的瀏覽器。
代理服務(wù)器不只是簡單地向服務(wù)器轉(zhuǎn)發(fā)請求,它還可以控制用戶的行為,對接收到的客戶請求進行決策,并根據(jù)過濾規(guī)則對用戶請求進行過濾
我使用spring boot,想快速的實現(xiàn)一個用于測試的代理服務(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è)置允許跨域請求
@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
啟動項目,訪問http://localhost:8801/,就會訪問到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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實現(xiàn)簡單學(xué)生管理系統(tǒng)項目
這篇文章主要介紹了java實現(xiàn)簡單學(xué)生管理系統(tǒng)項目,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07Java Base64算法實際應(yīng)用之郵件發(fā)送實例分析
這篇文章主要介紹了Java Base64算法實際應(yīng)用之郵件發(fā)送,結(jié)合實例形式分析了java字符編碼與郵件發(fā)送相關(guān)操作技巧,需要的朋友可以參考下2019-09-09