From dd150b1d2244d0665c2e52cedcdf3804ac52b7e3 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Fri, 17 Mar 2023 16:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=9F=8E=E5=B8=82=E7=9F=A2=E9=87=8F=E8=BE=B9?= =?UTF-8?q?=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/common/config/WebSecurityConfig.java | 1 + .../controller/VectorBoundaryController.java | 33 +++++++++++++++++++ .../com/xkrs/utilsold/HttpClientUtils.java | 19 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/main/java/com/xkrs/controller/VectorBoundaryController.java diff --git a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java index c4b8856..fb3c5c8 100644 --- a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java +++ b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java @@ -57,6 +57,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers(HttpMethod.GET, "/api/adm/getCityList").permitAll()//根据省编号获取市列表 .antMatchers(HttpMethod.GET, "/api/adm/getCountyList").permitAll()//根据市编号获取区县列表 .antMatchers(HttpMethod.GET, "/api/adm/getStreetList").permitAll()//根据区县编号获取街道列表 + .antMatchers(HttpMethod.GET, "/queryVectorBoundary").permitAll()//获取城市矢量边界 // 所有其它请求需要身份认证 .anyRequest().authenticated() .and() diff --git a/src/main/java/com/xkrs/controller/VectorBoundaryController.java b/src/main/java/com/xkrs/controller/VectorBoundaryController.java new file mode 100644 index 0000000..9a97aae --- /dev/null +++ b/src/main/java/com/xkrs/controller/VectorBoundaryController.java @@ -0,0 +1,33 @@ +package com.xkrs.controller; + +import com.xkrs.utilsold.HttpClientUtils; +import org.apache.hc.core5.util.TextUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class VectorBoundaryController { + + @GetMapping("/queryVectorBoundary") + public String queryVectorBoundary(@RequestParam(value = "tk", required = false) String tk, + @RequestParam(value = "lon") Double lon, + @RequestParam(value = "lat") Double lat, + @RequestParam(value = "scale") Double scale, + @RequestParam(value = "bgeo", required = false) Boolean bgeo) throws Exception { + String defaultTk = "9cc3e9deb3cf643b6f133717c333d16d"; + Boolean defaultBgeo = true; + String finalTk = (TextUtils.isEmpty(tk)) ? defaultTk : tk; + Boolean finalBgeo = (bgeo == null) ? defaultBgeo : bgeo; + + StringBuilder builder = new StringBuilder("https://service.sdmap.gov.cn/citysearch"); + builder.append("?tk=").append(finalTk); + builder.append("&lon=").append(lon); + builder.append("&lat=").append(lat); + builder.append("&scale=").append(scale); + builder.append("&bgeo=").append(finalBgeo); + String url = builder.toString(); + return HttpClientUtils.sendHttpsGetCustom(url); + } + +} diff --git a/src/main/java/com/xkrs/utilsold/HttpClientUtils.java b/src/main/java/com/xkrs/utilsold/HttpClientUtils.java index dbcdbb6..71b098c 100644 --- a/src/main/java/com/xkrs/utilsold/HttpClientUtils.java +++ b/src/main/java/com/xkrs/utilsold/HttpClientUtils.java @@ -62,6 +62,25 @@ public class HttpClientUtils { return doGet(url, httpClient); } + /** + * 发送https+get请求,绕过证书 + * + * @param url 请求地址 + * @return 返回结果 + * @throws Exception + */ + public static String sendHttpsGetCustom(String url) throws Exception { + SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE); + CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build(); + if (printLog) { + log.info("HGet请求url={}", url); + } + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Referer", "http://sdmap.gov.cn/"); + httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"); + return execute(httpClient, httpGet); + } + /** * 发送http+post请求 *