接收到微信验证请求 返回纯文本

This commit is contained in:
wangw 2025-10-20 16:42:50 +08:00
parent 166d550fe6
commit 0b710bd391
2 changed files with 48 additions and 32 deletions

View File

@ -2,11 +2,14 @@ package com.czg.controller;
import com.czg.account.service.UserInfoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
/**
@ -27,38 +30,51 @@ public class NotifyController {
private static final String TOKEN = "DZjyHBq3nTujF0NM1dnXikU8ZCvy";
@GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
public String notify(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr) {
log.info("接收到微信验证请求 - signature: {}, timestamp: {}, nonce: {}, echostr: {}",
signature, timestamp, nonce, echostr);
return echostr;
@RequestMapping(produces = MediaType.TEXT_PLAIN_VALUE)
public String notify(HttpServletRequest request,
@RequestParam(required = false) String signature, // GET 必传POST 可选设为非必选
@RequestParam(required = false) String timestamp,
@RequestParam(required = false) String nonce,
@RequestParam(required = false) String echostr) {
// 1. 处理 GET 请求微信开发者验证
if ("GET".equals(request.getMethod())) {
log.info("接收到微信验证请求 - signature: {}, timestamp: {}, nonce: {}, echostr: {}",
signature, timestamp, nonce, echostr);
return echostr;
}
// 2. 处理 POST 请求微信消息推送如关注消息等
else if ("POST".equals(request.getMethod())) {
log.info("接收到微信 POST 消息请求 - signature: {}, timestamp: {}, nonce: {}",
signature, timestamp, nonce);
// 读取 POST 请求体中的 XML 数据微信推送的消息格式为 XML
String xmlData = readPostXml(request);
log.info("微信 POST 消息内容: {}", xmlData);
// TODO: 后续可添加消息解析业务处理逻辑如关注事件文本消息回复等
// 处理完成后微信要求返回 "SUCCESS" 或空字符串否则会重试推送
return SUCCESS;
}
// 3. 其他请求方式 PUT/DELETE返回空字符串
return "";
}
/**
* 工具方法读取 POST 请求体中的 XML 数据
*/
private String readPostXml(HttpServletRequest request) {
StringBuilder xmlSb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
xmlSb.append(line);
}
} catch (IOException e) {
log.error("读取微信 POST 消息体失败", e);
}
return xmlSb.toString();
}
// // 1. 校验参数完整性
// if (signature == null || timestamp == null || nonce == null) {
// log.error("验证失败:参数不完整");
// return "";
// }
//
// // 2. 将tokentimestampnonce按字典序排序
// String[] arr = new String[]{TOKEN, timestamp, nonce};
// Arrays.sort(arr);
//
// // 3. 拼接为一个字符串
// StringBuilder sb = new StringBuilder();
// for (String s : arr) {
// sb.append(s);
// }
//
// // 4. SHA1加密
// String encryptedStr = DigestUtils.sha1Hex(sb.toString());
// log.info("本地加密后签名: {}", encryptedStr);
//
// if (encryptedStr.equals(signature)) {
// return echostr;
// } else {
// log.error("签名验证失败 - 本地加密: {}, 微信签名: {}", encryptedStr, signature);
// return "";
// }
}

View File

@ -110,7 +110,7 @@ public class AcAccountUtil {
bodyJson.put("action_name", "QR_SCENE");
JSONObject actionInfo = new JSONObject();
JSONObject scene = new JSONObject();
scene.put("scene_id", 36449);
scene.put("scene_id", 333);
actionInfo.put("scene", scene);
bodyJson.put("action_info", actionInfo);
System.out.println(bodyJson);