From 7fc28bba7bc2d74c72cf351dcb20f1f9126a9c93 Mon Sep 17 00:00:00 2001 From: shiyi Date: Mon, 15 Jul 2024 16:04:23 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A4=A9=E5=9C=B0=E5=9B=BE=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=BB=93=E6=9E=9C=E6=95=B0=E6=8D=AE=E7=B1=BB=E9=9B=86?= =?UTF-8?q?=E4=B8=AD=E5=88=B0=E4=B8=80=E4=B8=AA=E7=B1=BB=E4=B8=AD=EF=BC=9B?= =?UTF-8?q?2.=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=9C=A8=E5=85=B6=E4=B8=AD=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AF=B7=E6=B1=82=E6=A0=A1=E9=AA=8C=EF=BC=8C?= =?UTF-8?q?=E5=86=85=E9=83=A8=E6=9C=8D=E5=8A=A1=E6=A0=A1=E9=AA=8Cip?= =?UTF-8?q?=EF=BC=8C=E5=85=B6=E4=BB=96=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=AF=B9=E8=AF=B7=E6=B1=82=E5=A4=B4=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=EF=BC=88=E5=85=B7=E4=BD=93=E6=A0=A1=E9=AA=8C=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=BE=85=E5=AE=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/InterfaceInterceptor.java | 100 ++++++++++++++++++ .../config/interceptor/WebAppConfigurer.java | 35 ++++++ .../web/pojo/cma/AddressComponent.java | 34 ------ .../web/pojo/cma/AntiGeoCodeResponse.java | 28 +++++ .../service/surfaceapi/CmaServiceImpl.java | 11 +- .../main/resources/application-weather.yml | 2 +- .../surfaceapi/CmaServiceImplTest.java | 4 +- 7 files changed, 170 insertions(+), 44 deletions(-) create mode 100644 weather-service/src/main/java/com/htfp/weather/web/config/interceptor/InterfaceInterceptor.java create mode 100644 weather-service/src/main/java/com/htfp/weather/web/config/interceptor/WebAppConfigurer.java delete mode 100644 weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AddressComponent.java diff --git a/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/InterfaceInterceptor.java b/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/InterfaceInterceptor.java new file mode 100644 index 0000000..8522ea7 --- /dev/null +++ b/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/InterfaceInterceptor.java @@ -0,0 +1,100 @@ +package com.htfp.weather.web.config.interceptor; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.crypto.Mac; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Formatter; + +/** + * @Author : shiyi + * @Date : 2024/7/5 10:26 + * @Description : 请求过滤 + */ +@Slf4j @Component +public class InterfaceInterceptor implements HandlerInterceptor { + + private static final Integer SECONDS = 60 * 1000; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String servletPath = request.getServletPath(); + // log.info("InterfaceInterceptor.preHandle >> 进入拦截, {}, {}", servletPath, handler.getClass().getName()); + // ip 校验 + // if (checkIp(request)) { + // return true; + // } + // 签名校验 + final String signature = request.getHeader("signature"); + final String timestamp = String.valueOf(request.getHeader("timestamp")); + final String requestURI = request.getRequestURI(); + String checkSignature = createSignature(timestamp, requestURI); + + if (!checkSignature.equalsIgnoreCase(signature)) { + return false; + } + + //判断时间是否是60s内 + long nowTime = System.currentTimeMillis(); + long reqTime = Long.parseLong(timestamp); + if (Math.abs(nowTime - reqTime) > SECONDS) { + return false; + } + + return true; + } + + private boolean checkIp(HttpServletRequest request) { + String remoteAddr = request.getRemoteAddr(); + if ("127.0.0.1".equals(remoteAddr) || "0:0:0:0:0:0:0:1".equals(remoteAddr)) { + return true; + } + if (remoteAddr.startsWith("172.10.0.")) { + // 局域网测试 + return true; + } + if ("182.92.130.23".equals(remoteAddr)) { + // 本地服务 + return true; + } + return false; + } + + /** + * @param key - 时间戳(需传入字符串) + * @param data- 接口路径,例如 /htfp/weather/upper/queryPlaneGrid + * @return + * @throws NoSuchAlgorithmException + * @throws InvalidKeyException + */ + private String createSignature(String key, String data) throws NoSuchAlgorithmException, InvalidKeyException { + // TODO 签名算法待定 + SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); + Mac mac = Mac.getInstance("HmacSHA1"); + mac.init(signingKey); + byte[] bytes = mac.doFinal(data.getBytes()); + return toHexString(bytes); + } + + /** + * 16进制编码,便于地面站发送请求 + * @param bytes + * @return + */ + private String toHexString(byte[] bytes) { + Formatter formatter = new Formatter(); + for (byte b : bytes) { + formatter.format("%02x", b); + } + return formatter.toString(); + } + +} + diff --git a/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/WebAppConfigurer.java b/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/WebAppConfigurer.java new file mode 100644 index 0000000..a13f445 --- /dev/null +++ b/weather-service/src/main/java/com/htfp/weather/web/config/interceptor/WebAppConfigurer.java @@ -0,0 +1,35 @@ +package com.htfp.weather.web.config.interceptor; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.annotation.Resource; + +/** + * @Author : shiyi + * @Date : 2024/7/5 11:41 + * @Description : + */ +@Configuration +public class WebAppConfigurer implements WebMvcConfigurer { + + @Resource + private InterfaceInterceptor interfaceInterceptor; + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 可以添加多个拦截器,一般只添加一个 + // addPathPatterns("/**") 表示对所有请求都拦截 + // .excludePathPatterns("/base/index") 表示排除对/base/index请求的拦截 + // 多个拦截器可以设置order顺序,值越小,preHandle越先执行,postHandle和afterCompletion越后执行 + // order默认的值是0,如果只添加一个拦截器,可以不显示设置order的值 + registry.addInterceptor(interfaceInterceptor) + .addPathPatterns("/**") + // .excludePathPatterns("/base/index") + .order(0); + + } + + +} + diff --git a/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AddressComponent.java b/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AddressComponent.java deleted file mode 100644 index 9d6bf58..0000000 --- a/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AddressComponent.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.htfp.weather.web.pojo.cma; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Data; - -@Data @JsonIgnoreProperties(ignoreUnknown = true) -public class AddressComponent { - - private String address; - private String city; - @JsonProperty("county_code") - private String countyCode; - private String nation; - @JsonProperty("poi_position") - private String poiPosition; - private String county; - @JsonProperty("city_code") - private String cityCode; - @JsonProperty("address_position") - private String addressPosition; - private String poi; - @JsonProperty("province_code") - private String provinceCode; - private String province; - private String road; - @JsonProperty("road_distance") - private int roadDistance; - @JsonProperty("poi_distance") - private int poiDistance; - @JsonProperty("address_distance") - private int addressDistance; - private String info; -} diff --git a/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AntiGeoCodeResponse.java b/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AntiGeoCodeResponse.java index fd3bbf1..2393764 100644 --- a/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AntiGeoCodeResponse.java +++ b/weather-service/src/main/java/com/htfp/weather/web/pojo/cma/AntiGeoCodeResponse.java @@ -1,6 +1,7 @@ package com.htfp.weather.web.pojo.cma; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; /** @@ -16,6 +17,33 @@ public class AntiGeoCodeResponse { @Data @JsonIgnoreProperties(value = {"formattedAddress", "location"}, ignoreUnknown = true) public static class Result { AddressComponent addressComponent; + @Data @JsonIgnoreProperties(ignoreUnknown = true) + public static class AddressComponent { + private String address; + private String city; + @JsonProperty("county_code") + private String countyCode; + private String nation; + @JsonProperty("poi_position") + private String poiPosition; + private String county; + @JsonProperty("city_code") + private String cityCode; + @JsonProperty("address_position") + private String addressPosition; + private String poi; + @JsonProperty("province_code") + private String provinceCode; + private String province; + private String road; + @JsonProperty("road_distance") + private int roadDistance; + @JsonProperty("poi_distance") + private int poiDistance; + @JsonProperty("address_distance") + private int addressDistance; + private String info; + } } } diff --git a/weather-service/src/main/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImpl.java b/weather-service/src/main/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImpl.java index 09d3460..e86e119 100644 --- a/weather-service/src/main/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImpl.java +++ b/weather-service/src/main/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImpl.java @@ -5,8 +5,8 @@ import com.htfp.weather.utils.DateTimeUtils; import com.htfp.weather.utils.JSONUtils; import com.htfp.weather.web.exception.AppException; import com.htfp.weather.web.exception.ErrorCode; -import com.htfp.weather.web.pojo.cma.AddressComponent; import com.htfp.weather.web.pojo.cma.AntiGeoCodeResponse; +import com.htfp.weather.web.pojo.cma.AntiGeoCodeResponse.Result.AddressComponent; import com.htfp.weather.web.pojo.cma.CmaWarning; import com.htfp.weather.web.pojo.response.NowWeatherStatus; import com.htfp.weather.web.pojo.response.SurfaceWeatherWarning; @@ -54,10 +54,10 @@ public class CmaServiceImpl implements ISurfaceDataService{ @Override public List getSurfaceWarning(double lat, double lon) throws Exception { String countyId = getCountyIdByLatLon(lat, lon); - return getSurfaceWarningByCounty(countyId); + return getSurfaceWarningByCountyCode(countyId); } - public List getSurfaceWarningByCounty(String countyCode) { + public List getSurfaceWarningByCountyCode(String countyCode) { if (warningCache.containsKey(countyCode)) { return warningCache.get(countyCode); } else { @@ -113,7 +113,7 @@ public class CmaServiceImpl implements ISurfaceDataService{ params.put("startTime", startTime.format(formatter)); params.put("endTime", endTime.format(formatter)); params.put("provinceCode", countyCode); - log.info("更新预警数据 start:{}", params); + log.info("更新预警数据: {}", params); try { if (params.get("provinceCode") == null) { warningCache.clear(); @@ -134,9 +134,8 @@ public class CmaServiceImpl implements ISurfaceDataService{ } } } catch (Exception e) { - log.error("[中央气象台] 预警信息请求结果处理错误, {}", e.getMessage()); + log.error("[中央气象台] 预警信息请求结果处理错误, 预警数据更新失败, {}", e.getMessage()); } - log.info("更新预警数据end"); } private SurfaceWeatherWarning buildSurfaceWeatherWarning(CmaWarning cmaWarning) { diff --git a/weather-service/src/main/resources/application-weather.yml b/weather-service/src/main/resources/application-weather.yml index c3184e6..b374290 100644 --- a/weather-service/src/main/resources/application-weather.yml +++ b/weather-service/src/main/resources/application-weather.yml @@ -4,7 +4,7 @@ hefeng: caiyun: key: Tc9tgOYr5jlPPlEw tianditu: - key: 31094f552e1133bc7a82cfea7f03d74a + key: 86c4d7e4084ee8bf162a1fe9b62d982e # tablestore tablestore: endpoint: https://gfs-test.cn-hangzhou.ots.aliyuncs.com diff --git a/weather-service/src/test/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImplTest.java b/weather-service/src/test/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImplTest.java index fbec7b6..6c75f52 100644 --- a/weather-service/src/test/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImplTest.java +++ b/weather-service/src/test/java/com/htfp/weather/web/service/surfaceapi/CmaServiceImplTest.java @@ -7,8 +7,6 @@ import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; -import static org.junit.jupiter.api.Assertions.*; - /** * @Author : shiyi * @Date : 2024/7/2 16:43 @@ -36,7 +34,7 @@ class CmaServiceImplTest { @Test void updateCmaWarning() throws Exception { cmaService.updateCmaWarning(); - System.out.println(cmaService.getSurfaceWarningByCounty("310000")); + System.out.println(cmaService.getSurfaceWarningByCountyCode("310000")); } @Test