import org.apache.commons.io.FileUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
@RestController
public class ProxyController{
// 内网服务器地址+端口
private String targetAddr = "http://10.0.0.100:9000";
/**
* 代理所有请求
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/proxy/**")
public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
// String url = URLDecoder.decode(request.getRequestURL().toString(), "UTF-8");
URI uri = new URI(request.getRequestURI());
String path = uri.getPath();
String query = request.getQueryString();
String target = targetAddr + path.replace("/proxy", "");
if (query != null && !query.equals("") && !query.equals("null")) {
target = target + "?" + query;
}
URI newUri = new URI(target);
// 执行代理查询
String methodName = request.getMethod();
HttpMethod httpMethod = HttpMethod.resolve(methodName);
if (httpMethod == null) {
return;
}
InputStream stream = null;
String contentType = request.getContentType();
// 兼容文件上传的请求
if (contentType != null && contentType.startsWith("multipart/form-data")){
MultipartHttpServletRequest mulReq = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> map = mulReq.getFileMap();
List<MultipartFile> valueList = new ArrayList<MultipartFile>(map.values());
MultiValueMap<String, Object> params = new LinkedMultiValueMap();
for (MultipartFile file : valueList) {
File newFile = File.createTempFile("temp",file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
FileSystemResource resource = new FileSystemResource(newFile);
params.add(file.getName(), resource);
}
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println(headerName + ":" + request.getHeader(headerName));
headers.set(headerName,request.getHeader(headerName));
}
// 手动设置请求头的token信息
headers.set("Authorization",request.getHeader("Authorization"));
// 用HttpEntity封装整个请求报文
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<MultiValueMap<String, Object>>(params,headers);
String res = restTemplate.postForEntity(target, files, String.class).getBody();
InputStream is =new ByteArrayInputStream(res.getBytes("UTF-8"));
stream = is;
// 其他请求例如get post put delete都可使用
}else {
ClientHttpRequest delegate = new SimpleClientHttpRequestFactory().createRequest(newUri, httpMethod);
Enumeration<String> headerNames = request.getHeaderNames();
// 设置请求头
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
Enumeration<String> v = request.getHeaders(headerName);
List<String> arr = new ArrayList<>();
while (v.hasMoreElements()) {
arr.add(v.nextElement());
}
delegate.getHeaders().addAll(headerName, arr);
}
StreamUtils.copy(request.getInputStream(), delegate.getBody());
// 执行远程调用
ClientHttpResponse clientHttpResponse = delegate.execute();
response.setStatus(clientHttpResponse.getStatusCode().value());
// 设置响应头
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
response.setHeader(key, it);
}));
stream = clientHttpResponse.getBody();
}
// 将获取到的输入流再次输出到页面输出流中
StreamUtils.copy(stream, response.getOutputStream());
}
}
springboot将外网请求转发到内网服务器中并直接返回数据
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 用家里的电脑访问公司电脑 一、准备的东西 1.公网服务器(linux)2.公司内网电脑(windows)3.能连接...
- 几个Ip用于测试(txt文件夹位于/data/webdocs/pchi/tools/iptest.txt";10....
- server1:10.10.10.1:内网服务器;无代理 server2:10.10.10.2:内网服务器;有代理...
- UseCase: 在内网主机 或者内网主机上的虚拟机上 搭建服务器, (本案例为web server), 从外网(...