一 完成订阅号的认证
登录https://mp.weixin.qq.com/,当你已经成功创建自己的公众号,找到基本配置
二 springboot服务器搭建
controller接口
package com.example.tax.controller;
import javax.servlet.http.HttpServletRequest;
import com.example.tax.util.WeiXinSignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @Description: 微信请求处理的核心类
* @Parameters:
* @Return:
* @Create Date:
* @Version: V1.00
* @author: 来日可期
*/
@RestController
@RequestMapping(value="/wechat")
public class WeixinCoreController {
private static Logger logger = LoggerFactory.getLogger(WeixinCoreController.class);
@Autowired
private WeiXinSignUtil weixinSignUtil;
/**
* @Description: 验证请求是否来自微信服务器
* @Parameters: WeixinCoreController
* @Return: 返回微信服务器发过来的验证字符
* @Create Date:
* @Version: V1.00
* @author:来日可期
*/
@RequestMapping(value = "/access", method = RequestMethod.GET)
public String WeChatInterface(HttpServletRequest request) throws Exception {
System.out.println("-------------验证微信服务号信息开始----------");
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
logger.info("signature is :" + signature + "timestamp is" + timestamp + "nonce is :" + nonce);
if (weixinSignUtil.checkSignature(signature, timestamp, nonce)) {
System.out.println("-----------验证微信服务号结束------------");
return echostr;
} else {
// 此处可以实现其他逻辑
logger.warn("不是微信服务器发过来的请求,请小心!");
return null;
}
}
}
微信请求校验工具类,token要和公众号的令牌一样
package com.example.tax.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
*
* @Description: 微信请求校验工具类
* @Parameters:
* @Return:
* @Create Date:
* @Version: V1.00
* @author: 来日可期
*/
@Component
public class WeiXinSignUtil {
public static Logger logger = LoggerFactory.getLogger(WeiXinSignUtil.class);
//与公众号中配置的TOKEN保持一致
public static String token = "weixin";
/**
* 验证签名
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public boolean checkSignature(String signature, String timestamp, String nonce){
String[] arr = new String[] { 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 {
//创建 MessageDigest对象,MessageDigest 通过其getInstance系列静态函数来进行实例化和初始化。
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
logger.info("执行微信签名加密认证");
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
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;
}
}
application.properties设置端口号,我这里用的是http请求,https则用443端口
server.port=80