昨天来了一个活,客户需要远程发送指令给我们的一台服务器控制启动,但是我们并不想直接让他们访问以免暴露接口。这时候就提出来,我中转服务器封装一个接口,隐藏真实接口。简化如下:
假设客户端是A,我此时服务器是B,客户想要访问的是C。但是不能让他直接访问C,所以这时候就需要A ——> B——>C
此时我需要接收A的调用,然后将A传来的数据发送到C,然后最好再把C返回的结果返回给A,就大功告成。
HTTP-Proxy-Servlet工具,比较成熟,github地址如下:
Smiley’s HTTP Proxy Servlet
下面讲一下我自己项目对它的使用情况。
我的项目是基于 SpringBoot2.x的,故采用案例介绍的使用方式。
org.mitre.dsmiley.httpproxy smiley-http-proxy-servlet 1.12.1
package com.xxx.config;import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;import java.util.Properties;@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {@Beanpublic ServletRegistrationBean servletRegistrationBean() {Properties properties= (Properties) bindResult.get();ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), properties.getProperty("servlet_url"));servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, properties.getProperty("target_url"));servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, properties.getProperty("logging_enabled", "false"));return servletRegistrationBean;}private BindResult bindResult;@Overridepublic void setEnvironment(Environment environment) {Iterable sources = ConfigurationPropertySources.get(environment);Binder binder = new Binder(sources);BindResult bindResult = binder.bind("proxy.solr", Properties.class);this.bindResult = bindResult;}
}
proxy:solr:servlet_url: /solr/* # 这里不能写ip地址,只写接口后半段即可target_url: http://solrserver:8983/solr # 真实服务器接口地址
postman访问本地服务地址,代理转发到另外一个服务器上,接口转发成功,且返回文件上传成功信息。
通过。
总之,好用又简单,感兴趣的可以自己研究自己造轮子~
附上几个这方面的博主博客地址,有自己写的: