1. 天地图响应结果数据类集中到一个类中;2.增加接口拦截器,在其中增加接口请求校验,内部服务校验ip,其他客户端请求对请求头校验(具体校验方法待定)

download
shiyi 11 months ago
parent 0e9ce60318
commit 7fc28bba7b

@ -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();
}
}

@ -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);
}
}

@ -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;
}

@ -1,6 +1,7 @@
package com.htfp.weather.web.pojo.cma; package com.htfp.weather.web.pojo.cma;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
/** /**
@ -16,6 +17,33 @@ public class AntiGeoCodeResponse {
@Data @JsonIgnoreProperties(value = {"formattedAddress", "location"}, ignoreUnknown = true) @Data @JsonIgnoreProperties(value = {"formattedAddress", "location"}, ignoreUnknown = true)
public static class Result { public static class Result {
AddressComponent addressComponent; 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;
}
} }
} }

@ -5,8 +5,8 @@ import com.htfp.weather.utils.DateTimeUtils;
import com.htfp.weather.utils.JSONUtils; import com.htfp.weather.utils.JSONUtils;
import com.htfp.weather.web.exception.AppException; import com.htfp.weather.web.exception.AppException;
import com.htfp.weather.web.exception.ErrorCode; 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;
import com.htfp.weather.web.pojo.cma.AntiGeoCodeResponse.Result.AddressComponent;
import com.htfp.weather.web.pojo.cma.CmaWarning; import com.htfp.weather.web.pojo.cma.CmaWarning;
import com.htfp.weather.web.pojo.response.NowWeatherStatus; import com.htfp.weather.web.pojo.response.NowWeatherStatus;
import com.htfp.weather.web.pojo.response.SurfaceWeatherWarning; import com.htfp.weather.web.pojo.response.SurfaceWeatherWarning;
@ -54,10 +54,10 @@ public class CmaServiceImpl implements ISurfaceDataService{
@Override @Override
public List<SurfaceWeatherWarning> getSurfaceWarning(double lat, double lon) throws Exception { public List<SurfaceWeatherWarning> getSurfaceWarning(double lat, double lon) throws Exception {
String countyId = getCountyIdByLatLon(lat, lon); String countyId = getCountyIdByLatLon(lat, lon);
return getSurfaceWarningByCounty(countyId); return getSurfaceWarningByCountyCode(countyId);
} }
public List<SurfaceWeatherWarning> getSurfaceWarningByCounty(String countyCode) { public List<SurfaceWeatherWarning> getSurfaceWarningByCountyCode(String countyCode) {
if (warningCache.containsKey(countyCode)) { if (warningCache.containsKey(countyCode)) {
return warningCache.get(countyCode); return warningCache.get(countyCode);
} else { } else {
@ -113,7 +113,7 @@ public class CmaServiceImpl implements ISurfaceDataService{
params.put("startTime", startTime.format(formatter)); params.put("startTime", startTime.format(formatter));
params.put("endTime", endTime.format(formatter)); params.put("endTime", endTime.format(formatter));
params.put("provinceCode", countyCode); params.put("provinceCode", countyCode);
log.info("更新预警数据 start{}", params); log.info("更新预警数据: {}", params);
try { try {
if (params.get("provinceCode") == null) { if (params.get("provinceCode") == null) {
warningCache.clear(); warningCache.clear();
@ -134,9 +134,8 @@ public class CmaServiceImpl implements ISurfaceDataService{
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("[中央气象台] 预警信息请求结果处理错误, {}", e.getMessage()); log.error("[中央气象台] 预警信息请求结果处理错误 预警数据更新失败, {}", e.getMessage());
} }
log.info("更新预警数据end");
} }
private SurfaceWeatherWarning buildSurfaceWeatherWarning(CmaWarning cmaWarning) { private SurfaceWeatherWarning buildSurfaceWeatherWarning(CmaWarning cmaWarning) {

@ -4,7 +4,7 @@ hefeng:
caiyun: caiyun:
key: Tc9tgOYr5jlPPlEw key: Tc9tgOYr5jlPPlEw
tianditu: tianditu:
key: 31094f552e1133bc7a82cfea7f03d74a key: 86c4d7e4084ee8bf162a1fe9b62d982e
# tablestore # tablestore
tablestore: tablestore:
endpoint: https://gfs-test.cn-hangzhou.ots.aliyuncs.com endpoint: https://gfs-test.cn-hangzhou.ots.aliyuncs.com

@ -7,8 +7,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource; import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* @Author : shiyi * @Author : shiyi
* @Date : 2024/7/2 16:43 * @Date : 2024/7/2 16:43
@ -36,7 +34,7 @@ class CmaServiceImplTest {
@Test @Test
void updateCmaWarning() throws Exception { void updateCmaWarning() throws Exception {
cmaService.updateCmaWarning(); cmaService.updateCmaWarning();
System.out.println(cmaService.getSurfaceWarningByCounty("310000")); System.out.println(cmaService.getSurfaceWarningByCountyCode("310000"));
} }
@Test @Test

Loading…
Cancel
Save