微信小程序消息推送token验证Java样例
//校验微信小程序消息token
public void checkSignature() {
try {
String timestamp = null != getPara("timestamp") ? getPara("timestamp") : ""; //时间戳
String nonce = null != getPara("nonce") ? getPara("nonce") : ""; //随机字符串
String signature = null != getPara("signature") ? getPara("signature") : ""; //签名
String echostr = null != getPara("echostr") ? getPara("echostr") : ""; //自定义字符串,验证成功后原样返回
String token = "ams2022"; //明文
String[] tokenArray = new String[]{timestamp, nonce, token};
Arrays.sort(tokenArray); //排序
//排序后的字符串
StringBuilder sb = new StringBuilder();
for (String s : tokenArray) {
sb.append(s);
}
String local_signature = HashKit.sha1(String.valueOf(sb)); //sha1加密
if (local_signature.equals(signature)) {
renderText(echostr); //验证成功,返回echostr给微信服务器
} else {
renderText(String.valueOf(false));
}
} catch (Exception e) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bs));
renderText(String.valueOf(false));
}
}
1、小程序消息推送配置
2、验证配置
本文来自博客园,作者:七月的枫丶 ,部分内容摘自互联网,转载请注明原文链接:https://www.cnblogs.com/easybook/p/16135325.html