项目基于Spring
须要的依赖为:
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>
只需要添加一个java文件即可,原理有待继续研究
package *.*.*.*;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by czz on 2018/11/13.
*/
@Configuration
public class SolrProxyServletConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
}
还可以通过配置多个bean来达到代理多个服务的目的
package *.*.*.*;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by czz on 2018/11/13.
*/
@Configuration
public class SolrProxyServletConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
servletRegistrationBean.setName("baidu");
servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean servletRegistrationBean1(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/bootstrap/*");
servletRegistrationBean.setName("runoob");
servletRegistrationBean.addInitParameter("targetUri", "http://www.runoob.com/bootstrap");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean servletRegistrationBean2(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/otheragent/*");
servletRegistrationBean.setName("***");
servletRegistrationBean.addInitParameter("targetUri", "http://***/otheragent");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
return servletRegistrationBean;
}
}
参考文章:Spring boot使用ProxyFilter进行服务代理
希望可以帮助到大家,大家也可关注我的公众号方便在手机上进行查看