| import io.swagger.annotations.Api; |
| import io.swagger.annotations.ApiOperation; |
| import lombok.extern.slf4j.Slf4j; |
| import org.springframework.web.bind.annotation.*; |
| |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import java.io.IOException; |
| import java.io.PrintWriter; |
| import java.security.MessageDigest; |
| import java.util.*; |
| |
| /** |
| * @description: 微信配置 |
| * @author: Mr.Fang |
| * @create: 2023-05-26 |
| **/ |
| @Api(tags = "微信配置") |
| @Slf4j |
| @RestController |
| @RequestMapping("/wx/") |
| public class WxController { |
| |
| String token="78******23"; |
| |
| @ApiOperation(value = "微信 token URL 验证") |
| @GetMapping(value = "push") |
| public void pushGet(HttpServletRequest request, HttpServletResponse response) { |
| String signature = request.getParameter("signature"); // 签名 |
| String echostr = request.getParameter("echostr"); // 随机字符串 |
| String timestamp = request.getParameter("timestamp"); // 时间戳 |
| String nonce = request.getParameter("nonce"); // 随机数 |
| log.debug("signature:{}", signature); |
| log.debug("echostr:{}", echostr); |
| log.debug("timestamp:{}", timestamp); |
| log.debug("nonce:{}", nonce); |
| System.out.println("signature:" + signature); |
| String sha1 = getSHA1(token, timestamp, nonce); |
| System.out.println("sha1:" + sha1); |
| if (sha1.equals(signature)) { |
| log.debug("成功"); |
| this.responseText(echostr, response); |
| } |
| } |
| |
| @ApiOperation(value = "接收微信事件") |
| @PostMapping(value = "push") |
| public void pushPost(HttpServletRequest request, HttpServletResponse response) { |
| String signature = request.getParameter("signature"); // 签名 |
| String timestamp = request.getParameter("timestamp"); // 时间戳 |
| String nonce = request.getParameter("nonce"); // 随机数 |
| String sha1 = getSHA1(token, timestamp, nonce); |
| if (sha1.equals(signature)) { |
| Map<String, Object> map = null; |
| try { |
| // input 流返回是 xml 格式 这里转 map 了 |
| map = XmlUtil.parseXMLToMap(request.getInputStream()); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| log.debug("事件消息体:{}", map); |
| |
| this.responseText("", response); // 回复空串,微信服务器不会对此作任何处理 |
| } |
| |
| } |
| |
| /** |
| * 返回响应结果 |
| * |
| * @param text 响应内容 |
| * @param response |
| */ |
| public void responseText(String text, HttpServletResponse response) { |
| response.setCharacterEncoding("UTF-8"); |
| response.setContentType("text/plan;charset=UTF-8"); |
| PrintWriter writer = null; |
| try { |
| writer = response.getWriter(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| writer.write(text); |
| writer.flush(); |
| writer.close(); |
| } |
| |
| /** |
| * 用SHA1算法生成安全签名 |
| * |
| * @param token 票据 |
| * @param timestamp 时间戳 |
| * @param nonce 随机字符串 |
| * @return 安全签名 |
| */ |
| public String getSHA1(String token, String timestamp, String nonce) { |
| try { |
| String[] array = new String[]{token, timestamp, nonce}; |
| StringBuffer sb = new StringBuffer(); |
| // 字符串排序 |
| Arrays.sort(array); |
| for (int i = 0; i < 3; i++) { |
| sb.append(array[i]); |
| } |
| String str = sb.toString(); |
| // SHA1签名生成 |
| MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| md.update(str.getBytes()); |
| byte[] digest = md.digest(); |
| |
| StringBuffer hexstr = new StringBuffer(); |
| String shaHex = ""; |
| for (int i = 0; i < digest.length; i++) { |
| shaHex = Integer.toHexString(digest[i] & 0xFF); |
| if (shaHex.length() < 2) { |
| hexstr.append(0); |
| } |
| hexstr.append(shaHex); |
| } |
| return hexstr.toString(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| } |
| |