package com.boman.wechat.controller; import com.boman.domain.dto.WxMsgDto; import com.boman.wechat.service.WxPushService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.util.Objects; import static com.boman.wechat.utils.CheckAuthUtils.*; /** * 微信登录验证 * * @author zhong.h */ @RestController @RequestMapping("/push/msg") public class PushMsgController { private static final Logger LOGGER = LoggerFactory.getLogger(PushMsgController.class); @Autowired private WxPushService wxPushService; @GetMapping public void pushMsg(HttpServletRequest request, HttpServletResponse response) { // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 String signature = request.getParameter(SIGNATURE); // 时间戳 String timestamp = request.getParameter(TIMESTAMP); // 随机数 String nonce = request.getParameter(NONCE); // 随机字符串 String echostr = request.getParameter(ECHOSTR); LOGGER.info("method: pushMsg, signature: {}, timestamp: {}, nonce: {}, echostr: {}", signature, timestamp, nonce, echostr); PrintWriter out = null; try { out = response.getWriter(); if (checkSignature(signature, timestamp, nonce)) { out.print(echostr); out.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { if (Objects.nonNull(out)) { out.close(); } } } @PostMapping("/pushMsg") public String pushMsg(@RequestBody WxMsgDto dto) { return wxPushService.pushToUser(dto.getIds(), dto.getParams()); } }