验证消息
This commit is contained in:
@@ -3,12 +3,14 @@ package com.czg.controller;
|
||||
import com.czg.account.service.UserInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 公众号 通知
|
||||
*
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@@ -21,10 +23,38 @@ public class NotifyController {
|
||||
|
||||
@Resource
|
||||
private UserInfoService userInfoService;
|
||||
private static final String TOKEN = "DZjyHBq3nTujF0NM1dnXikU8ZCvy";
|
||||
|
||||
@RequestMapping
|
||||
public String notify(@RequestBody String str) {
|
||||
log.info("公众号 通知:{}", str);
|
||||
return SUCCESS;
|
||||
|
||||
@GetMapping
|
||||
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);
|
||||
// 1. 校验参数完整性
|
||||
if (signature == null || timestamp == null || nonce == null) {
|
||||
log.error("验证失败:参数不完整");
|
||||
return "";
|
||||
}
|
||||
|
||||
// 2. 将token、timestamp、nonce按字典序排序
|
||||
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 "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user