公众号后台接口确认(让微信后台确认我们的后台是可用的)

    xiaoxiao2021-12-14  18

    /** * 测试公众号进行接口确认方法 * * @param request * @return */ public ModelAndView makeSureTestInterface(HttpServletRequest request) { String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); if (WXCheckUtils.checkSignature(signature, timestamp, nonce)) { System.out.println("成功获取"); return ajaxJson(echostr); } else { return ajaxJson(""); } }

    这里用到的springMVC的controller,其他框架可据此修改,

    /** * 微信公众号验证工具类 * @author Draven * @date 2016年9月28日 * @e-mail draven1122@163.com */ public class WXCheckUtils { private static final String TOKEN="draven"; public static boolean checkSignature(String signature,String timestamp,String nonce){ String[] arr=new String[]{TOKEN,timestamp,nonce}; Arrays.sort(arr); //生成字符串 StringBuffer sb=new StringBuffer(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); } //SHA1加密 String temp=SHA1(sb.toString()); return temp.equals(signature); } /** * SHA1加密 * @param decript * @return */ public static String SHA1(String decript) { try { MessageDigest digest = java.security.MessageDigest .getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }

    转载请注明原文地址: https://ju.6miu.com/read-962272.html

    最新回复(0)