package com.smtaiserver.smtaiserver.control;
|
|
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
|
import com.smtservlet.core.SMTRequest;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.springframework.boot.configurationprocessor.json.JSONObject;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
import static java.util.Arrays.sort;
|
public class SMTAIWeixinControl {
|
|
private static Logger _logger = LogManager.getLogger(SMTAIServerControl.class);
|
|
/**
|
* 微信验证
|
*
|
* @param tranReq
|
* @param request
|
* @return
|
* @throws Exception
|
*/
|
public String weChatVerification(SMTRequest tranReq, HttpServletRequest request) throws Exception {
|
_logger.info(tranReq);
|
// 获取微信请求参数
|
String signature = request.getParameter("signature");
|
String timestamp = request.getParameter("timestamp");
|
String nonce = request.getParameter("nonce");
|
String echostr = request.getParameter("echostr");
|
_logger.info(
|
"开始校验此次消息是否来自微信服务器,param->signature:{},\ntimestamp:{},\nnonce:{},\nechostr:{}",
|
signature,
|
timestamp,
|
nonce,
|
echostr);
|
// 需要验证的时候就启用
|
if (checkSignature(signature, timestamp, nonce)) {
|
return echostr;
|
}
|
return "";
|
}
|
|
/**
|
* 验证签名
|
*
|
* @param signature
|
* @param timestamp
|
* @param nonce
|
* @return
|
*/
|
public static boolean checkSignature(String signature, String timestamp, String nonce)
|
throws Exception {
|
Object weixinParam = SMTAIServerApp.getApp().getGlobalConfig("weixin_core_param", "false");
|
JSONObject weixinJson = (JSONObject) weixinParam;
|
Object appId = weixinJson.get("appId");
|
Object secret = weixinJson.get("secret");
|
String token = (String) weixinJson.get("token");
|
|
String[] arr = new String[] {token, timestamp, nonce};
|
// 将token、timestamp、nonce三个参数进行字典序排序
|
// Arrays.sort(arr);
|
sort(arr);
|
StringBuilder content = new StringBuilder();
|
for (String s : arr) {
|
content.append(s);
|
}
|
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) {
|
_logger.error("签名异常", e);
|
}
|
content = null;
|
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
|
|
return tmpStr != null && tmpStr.equals(signature.toUpperCase());
|
}
|
|
/**
|
* 将字节数组转换为十六进制字符串
|
*
|
* @param byteArray
|
* @return
|
*/
|
private static String byteToStr(byte[] byteArray) {
|
StringBuilder strDigest = new StringBuilder();
|
for (byte b : byteArray) {
|
strDigest.append(byteToHexStr(b));
|
}
|
return strDigest.toString();
|
}
|
|
/**
|
* 将字节转换为十六进制字符串
|
*
|
* @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;
|
}
|
}
|