package com.ruoyi.project.wechataccount;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
* @Description : 微信公众号 服务器配置 校验
* @Author : ChenYao(https://blog.csdn.net/chenyao1994)
* @Version : 1.0
* @Create : 2020/5/3 14:58
*/
@RestController
@RequestMapping("/wechat")
@Slf4j
public class WeChatValidationController {
private static String WE_CHAT_ACCOUNT_TOKEN;
/**
* @param token token
* @author : ChenYao
* 给静态变量注入值
* <p>You can use getToken() to get the value of token</p>
*/
@Value("${wechat.token}")
public void setToken(String token) {
WeChatValidationController.WE_CHAT_ACCOUNT_TOKEN = token;
}
/**
* @description: 验证消息的确来自微信服务器
* @author: ChenYao(https : / / blog.csdn.net / chenyao1994)
* @return: java.lang.String
* @CeateDate: 2020/5/3 15:04
* <p>
* 开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带参数如下表所示:
* <p>
* 参数 描述
* signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
* timestamp 时间戳
* nonce 随机数
* echostr 随机字符串
* 开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务
*/
@GetMapping("/weChatValidation")
public String weChatValidation(HttpServletRequest request) {
//微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
/// 微信加密签名
String signature = request.getParameter("signature");
/// 时间戳
String timestamp = request.getParameter("timestamp");
/// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
/* String signature = weChatAuthEntity.getSignature();
Long timestamp = weChatAuthEntity.getTimestamp();
//随机数
Integer nonce = weChatAuthEntity.getNonce();
//随机字符串
String echostr = weChatAuthEntity.getEchostr();*/
//参数校验
if (StringUtils.isEmpty(signature) || timestamp == null || nonce == null || StringUtils.isEmpty(echostr) || StringUtils.isEmpty(WE_CHAT_ACCOUNT_TOKEN)) {
log.error("微信校验 含有参数为空值");
return "";
}
log.info("signature : [{}]; timestamp : [{}]; nonce : [{}]; echostr : [{}]; token : [{}]", signature, timestamp, nonce, echostr, WE_CHAT_ACCOUNT_TOKEN);
boolean flag = WeChatValidationController.checkSignature(signature, timestamp, nonce);
log.info("flag : {}", flag);
if (flag) {
return echostr;
}
return "xiaoye";
}
/**
* 校验签名
*/
public static boolean checkSignature(String signature, String timestamp, String nonce) {
System.out.println("signature:" + signature + "timestamp:" + timestamp + "nonc:" + nonce);
String[] arr = new String[]{WE_CHAT_ACCOUNT_TOKEN, timestamp, nonce};
// 将token、timestamp、nonce三个参数进行字典序排序
Arrays.sort(arr);
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
System.out.println(tmpStr.equals(signature.toUpperCase()));
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 将字节转换为十六进制字符串
*
* @param mByte
* @return
*/
private static String byteToHexStr(byte mByte) {
char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
}
微信公众号 开发 服务器配置
最新推荐文章于 2022-01-25 11:51:24 发布