import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aurochsframework.boot.commons.api.CommonResult;
import org.aurochsframework.boot.commons.param.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/wechat-callback")
@Api(tags = "WechatCallBackController", description = "微信回调")
public class WechatCallBackController {
@Autowired
private WechatAuthUserService wechatAuthUserService;
@GetMapping("/call")
@ApiOperation("微信公众消息转发服务器 验证")
public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {
try {
PrintWriter print = response.getWriter();
print.write(echostr);
print.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ApiOperation("微信公众消息转发服务器 业务")
@PostMapping("/call")
public CommonResult<String> call(HttpServletRequest request){
try {
Map<String, String> map = XmlUtils.xmlToMap(request);
String eventType = map.get("Event");
map.forEach((key,val) -> log.info("{}:{}",key,val));
switch (eventType){
//关注
case "subscribe":
break;
//取消关注
case "unsubscribe":
String openId = map.get("FromUserName");
//WechatAuthUser byOpenId = wechatAuthUserService.findByOpenId(openId);
//byOpenId.setWxOpenId("");
//wechatAuthUserService.updateByExampleSelective(byOpenId, QueryParam.createQueryParam().and("id",byOpenId.getId()));
break;
}
} catch (Exception e) {
return CommonResult.failed(e.getMessage());
}
return CommonResult.success("");
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Formatter;
/**
* 微信消息转卖的验证工具
*/
public class CheckoutUtil {
// 与测试账号接口配置信息中的Token要一致
private static String token = "999";
/**
* 验证签名
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean checkSignature(String signature, String timestamp, String nonce) {
String[] arr = new String[] { token, timestamp, nonce };
Arrays.sort(arr);// 将token、timestamp、nonce三个参数进行字典序排序
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 = byteToHex(digest );
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.equals(signature) : false;
}
//十六进制字节数组转为字符串
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
}
最后在微信公众号开发平台设置相应的服务连接和token。用户关注或取消关注,发送消息就会转发到post的call方法里。