微信官方文档:
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
Demo:
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.io.UnsupportedEncodingException;
@RestController
@RequestMapping(value = "/weChat/callback")
public class WeChatCallBackController {
private static final Logger LOG = LoggerFactory.getLogger(WeChatCallBackController.class);
/**
* 校验信息是否是从微信服务器发出
*
* @param request
* @param response
*/
@GetMapping
public void get(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
LOG.error("UnsupportedEncodingException", e);
}
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
// 微信开发者模式配置Token
String token = "Token";
try (PrintWriter writer = response.getWriter()) {
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (WeChatPayUtil.checkSignature(token, signature, timestamp, nonce)) {
writer.write(echostr);
}else {
// 回复消息为空,必须做出下述回复,这样微信服务器才不会对此作任何处理,
// 并且不会发起重试,否则,将出现严重的错误提示。直接回复success(推荐方式)
writer.write("success");
}
} catch (AesException e) {
LOG.error("AesException", e);
} catch (IOException e) {
LOG.error("IOException", e);
}
}
/**
* 微信服务器处理消息
*
* @param request
* @param response
*/
@PostMapping
public void post(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
LOG.error("UnsupportedEncodingException", e);
}
// 加密 response
String respMessage = "加密response";
try (PrintWriter writer = response.getWriter()) {
if (StringUtils.isNotBlank(respMessage)) {
writer.write(respMessage);
} else {
writer.write("success");
}
} catch (Exception e) {
LOG.error("Exception", e);
}
}
}
/**
* 验证微信开发者模式回调Token
*
* @param token Token(令牌)
* @param msgSignature 签名串,对应URL参数的signature
* @param timeStamp 时间戳,对应URL参数的timestamp
* @param nonce 随机串,对应URL参数的nonce
* @return 是否为安全签名
* @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
*/
public static boolean checkSignature(String token, String msgSignature, String timeStamp, String nonce)
throws AesException {
String signature = SHA1.getSHA1(token, timeStamp, nonce);
if (!signature.equals(msgSignature)) {
throw new AesException(AesException.ValidateSignatureError);
}
return true;
微信后台配置接口URL,若提交成功即OK啦!
微信公众号和微信小程序通用