Browse Source

调整了外部接口加签逻辑验证

dev-V1.0.7
vincent 2 months ago
parent
commit
113ad9b76f
  1. 99
      zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/common/util/ApiSignDemo.java
  2. 2
      zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/controller/externalInterface/LongGangInterfaceController.java
  3. 4
      zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/handler/ApiCryptoFilter.java
  4. 20
      zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/handler/ApiSignInterceptor.java
  5. 2
      zdxt-web-server/zdxt-admin-web-server/src/main/resources/application.yml

99
zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/common/util/ApiSignDemo.java

@ -0,0 +1,99 @@
package com.zdxt.enforcementcode.common.util;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class ApiSignDemo {
private static final String APP_KEY = "4263693515";
private static final String APP_SECRET = "e4Ugj6LytHX52AXizuNvanwZkVqXAB4J";
private static final String BASE_URL = "http://localhost:8080";
public static void main(String[] args) throws Exception {
System.out.println("========================================");
System.out.println(" 模拟第三方请求 - 完整加签示例");
System.out.println("========================================\n");
// ① 生成签名参数
String timestamp = String.valueOf(System.currentTimeMillis());
String nonce = IdUtil.fastSimpleUUID();
Map<String, String> signParams = new HashMap<>();
signParams.put("appKey", APP_KEY);
signParams.put("timestamp", timestamp);
signParams.put("nonce", nonce);
String signContent = ApiSignUtil.buildSignContent(signParams);
String sign = ApiSignUtil.hmacSign(signContent, APP_SECRET);
System.out.println("【签名参数】");
System.out.println(" App-Key: " + APP_KEY);
System.out.println(" Timestamp: " + timestamp);
System.out.println(" Nonce: " + nonce);
System.out.println(" 签名原文: " + signContent);
System.out.println(" Sign: " + sign);
System.out.println();
// ② 构建请求体
JSONObject body = new JSONObject();
body.put("planType", 1);
body.put("planDate", "2026-09-03");
body.put("planTime", 1);
body.put("enforceContent", "日常环保执法检查0904");
body.put("enterpriseName", "深新粤云(深圳)信息技术有限公司圳市XX科技有限公司");
body.put("enterpriseNumber", "91440300769156389U");
body.put("contactPerson", "张三");
body.put("contactPhone", "13800138000");
body.put("enforceUserNames", "测试1,测试2");
body.put("enforceUserIds", "17608417058,17608416858");
body.put("deptId", "20210922212955057-A601-27251E588");
body.put("deptName", "深圳市司法局");
body.put("createBy", "17608417058");
body.put("remark", "测试数据");
body.put("outSerialNo", "LG20260903001");
System.out.println("【请求体】");
System.out.println(" " + body.toJSONString());
System.out.println();
// ③ 发起 HTTP 请求
String url = BASE_URL + "/external/api/lg/plan/saveOrUpdatePlan";
System.out.println("【发送请求】");
System.out.println(" URL: " + url);
HttpResponse response = HttpRequest.post(url)
.header("App-Key", APP_KEY)
.header("Timestamp", timestamp)
.header("Nonce", nonce)
.header("Sign", sign)
.header("Content-Type", "application/json;charset=UTF-8")
.body(body.toJSONString())
.timeout(10000)
.execute();
System.out.println(" 状态码: " + response.getStatus());
System.out.println();
// ④ 输出响应
String responseBody = response.body();
System.out.println("【响应结果】");
try {
JSONObject json = JSONObject.parseObject(responseBody);
System.out.println(" " + json.toString());
} catch (Exception e) {
System.out.println(" " + responseBody);
}
response.close();
System.out.println();
System.out.println("========================================");
System.out.println(" 请求完成");
System.out.println("========================================");
}
}

2
zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/controller/externalInterface/LongGangInterfaceController.java

@ -40,7 +40,7 @@ import java.util.stream.Collectors;
@RequestMapping("/external/api/lg") @RequestMapping("/external/api/lg")
public class LongGangInterfaceController { public class LongGangInterfaceController {
private static final String HEADER_APP_KEY = "X-App-Key"; private static final String HEADER_APP_KEY = "App-Key";
private final IOutEfcodeCpPlanService outEfcodeCpPlanService; private final IOutEfcodeCpPlanService outEfcodeCpPlanService;
private final IOutEfcodeCpTaskService outEfcodeCpTaskService; private final IOutEfcodeCpTaskService outEfcodeCpTaskService;

4
zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/handler/ApiCryptoFilter.java

@ -28,8 +28,8 @@ public class ApiCryptoFilter implements Filter {
private final ApiPlatformConfig apiPlatformConfig; private final ApiPlatformConfig apiPlatformConfig;
private final ISysApiPlatformConfigService sysApiPlatformConfigService; private final ISysApiPlatformConfigService sysApiPlatformConfigService;
private static final String HEADER_ENCRYPTED = "X-Encrypted"; private static final String HEADER_ENCRYPTED = "Encrypted";
private static final String HEADER_APP_KEY = "X-App-Key"; private static final String HEADER_APP_KEY = "App-Key";
@Override @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

20
zdxt-modules/zdxt-enforcement-code/src/main/java/com/zdxt/enforcementcode/handler/ApiSignInterceptor.java

@ -27,10 +27,10 @@ public class ApiSignInterceptor implements HandlerInterceptor {
private final ApiPlatformConfig apiPlatformConfig; private final ApiPlatformConfig apiPlatformConfig;
private final ISysApiPlatformConfigService sysApiPlatformConfigService; private final ISysApiPlatformConfigService sysApiPlatformConfigService;
private static final String HEADER_APP_KEY = "X-App-Key"; private static final String HEADER_APP_KEY = "App-Key";
private static final String HEADER_TIMESTAMP = "X-Timestamp"; private static final String HEADER_TIMESTAMP = "Timestamp";
private static final String HEADER_NONCE = "X-Nonce"; private static final String HEADER_NONCE = "Nonce";
private static final String HEADER_SIGN = "X-Sign"; private static final String HEADER_SIGN = "Sign";
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
@ -44,7 +44,7 @@ public class ApiSignInterceptor implements HandlerInterceptor {
String sign = request.getHeader(HEADER_SIGN); String sign = request.getHeader(HEADER_SIGN);
if (StrUtil.hasBlank(appKey, timestamp, nonce, sign)) { if (StrUtil.hasBlank(appKey, timestamp, nonce, sign)) {
writeError(response, "缺少必要的签名参数: X-App-Key, X-Timestamp, X-Nonce, X-Sign"); writeError(response, "缺少必要的签名参数: App-Key, Timestamp, Nonce, Sign");
return false; return false;
} }
@ -67,8 +67,14 @@ public class ApiSignInterceptor implements HandlerInterceptor {
return false; return false;
} }
long now = System.currentTimeMillis() / 1000; long now = System.currentTimeMillis();
if (Math.abs(now - ts) > apiPlatformConfig.getTimestampExpireSeconds()) { long diff = now - ts;
log.info("当前时间: {}", now);
log.info("ts: {}", ts);
log.info("时间戳差值: {}", diff);
log.info("时间差: {}", Math.abs(now - ts));
log.info("配置的时间: {}", apiPlatformConfig.getTimestampExpireSeconds() * 1000);
if (Math.abs(now - ts) > apiPlatformConfig.getTimestampExpireSeconds() * 1000) {
writeError(response, "请求已过期,timestamp 超时"); writeError(response, "请求已过期,timestamp 超时");
return false; return false;
} }

2
zdxt-web-server/zdxt-admin-web-server/src/main/resources/application.yml

@ -339,7 +339,7 @@ http:
# 外部接口平台配置 # 外部接口平台配置
api-platform: api-platform:
enabled: true enabled: true
timestamp-expire-seconds: 300 timestamp-expire-seconds: 36000
consumers: consumers:
partner-a: partner-a:
app-key: ak_partner_a_xxxx app-key: ak_partner_a_xxxx

Loading…
Cancel
Save