|
|
@@ -0,0 +1,86 @@
|
|
|
+package com.skyversation.xjcy.controller;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.client.DefaultResponseErrorHandler;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.net.URI;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/proxy")
|
|
|
+@Slf4j
|
|
|
+public class ProxyController {
|
|
|
+ @Value("${app.ows.path}")
|
|
|
+ private String geoServerPath;
|
|
|
+
|
|
|
+ private final RestTemplate restTemplate = restTemplate();
|
|
|
+
|
|
|
+ public RestTemplate restTemplate() {
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
|
|
|
+ @Override
|
|
|
+ protected boolean hasError(HttpStatus statusCode) {
|
|
|
+ return false; // 任何状态码都当成成功,把控制权交还给调用方
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return restTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+// @GetMapping("/geoserver/**")
|
|
|
+// public ResponseEntity<?> proxyGeoserver(ProxyExchange<byte[]> proxy, @RequestParam Map<String,String> queryParams) throws Exception {
|
|
|
+// String remainingPath = proxy.path("/proxy/geoserver/");
|
|
|
+//
|
|
|
+// // 构建完整目标 URI
|
|
|
+// String targetBase = geoServerPath + "/" + remainingPath; // 注意 geoServerPath 不要以 / 结尾
|
|
|
+// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(targetBase);
|
|
|
+// MultiValueMap<String, String> queryParam = new LinkedMultiValueMap<>();
|
|
|
+// queryParams.forEach(queryParam::add);
|
|
|
+// builder.queryParams(queryParam);
|
|
|
+// URI finalUri = builder.build().toUri();
|
|
|
+//
|
|
|
+// // 打印最终转发的 URL(用于调试)
|
|
|
+// System.out.println("Forwarding to: " + finalUri.toString());
|
|
|
+//
|
|
|
+// // 执行转发
|
|
|
+// return proxy.uri(finalUri).get();
|
|
|
+// }
|
|
|
+
|
|
|
+ @GetMapping("/geoserver/**")
|
|
|
+ public String proxyGeoserver(HttpServletRequest request,
|
|
|
+ @RequestParam MultiValueMap<String, String> queryParams) throws Exception {
|
|
|
+
|
|
|
+ // 1. 提取 /geoserver/ 之后的剩余路径
|
|
|
+ String prefix = "/geoserver/";
|
|
|
+ String requestURI = request.getRequestURI();
|
|
|
+ int index = requestURI.indexOf(prefix);
|
|
|
+ String remainingPath = index >= 0 ? requestURI.substring(index + prefix.length()) : "";
|
|
|
+
|
|
|
+ // 2. 构建目标 URL
|
|
|
+ String targetBase = geoServerPath + "/" + remainingPath; // geoServerPath 不要以 / 结尾
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(targetBase);
|
|
|
+ if (queryParams != null && !queryParams.isEmpty()) {
|
|
|
+ builder.queryParams(queryParams);
|
|
|
+ }
|
|
|
+ URI finalUri = builder.build().toUri();
|
|
|
+
|
|
|
+ log.info("Forwarding to: {}", finalUri.toString());
|
|
|
+
|
|
|
+ // 3. 服务端自行发起 GET 请求,并直接返回目标服务的响应体
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ finalUri, HttpMethod.GET, null,String.class);
|
|
|
+
|
|
|
+ // 保留目标服务返回的状态码和关键头(特别是 Content-Type),只返回 body
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+}
|