微信公众测试好 TOKEN 配置用SpringBoot 2.x

微信公众测试好 TOKEN 配置用SpringBoot 2.x

第一步 写个链接接口

@Controller
public class WeChatAction {
    /**
     * 链接地址要加这个 http://你的域名/Token
     */
    @RequestMapping("Token")
    public void weChat(Model model, HttpServletRequest request,HttpServletResponse response) throws IOException {
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        System.out.println(isGet);
        PrintWriter print;
        if (isGet) {
            // 微信发的加密签名
            String signature = request.getParameter("signature");
            // 微信发的时间戳
            String timestamp = request.getParameter("timestamp");
            // 微信发的随机数
            String nonce = request.getParameter("nonce");
            // 随机字符串
            String echostr = request.getParameter("echostr");
            // 通过signature 检查 对比你跟你生产的signature 如果通过了就返回 echostr
            if (signature != null && CheckUtil.verifySignature(signature, timestamp, nonce)) {
                try {
                    print = response.getWriter();
                    print.write(echostr);
                    print.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(response.);
            }

        }

    }

第二步 写CheckUtil

public class CheckUtil {
   // 和在微信公众测试 TOKEN 要一样
   private static String token = "jesse";


   /**
    * 检验你的你的 signature 和微信官方发的 signature
    */
   public static boolean verifySignature(String signature, String timestamp, String nonce) {
       String[] arr = new String[] { token, timestamp, nonce };
       // 将token、timestamp、nonce三个参数进行字典序排序
       // Arrays.sort(arr);
       sort(arr);
       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 = byteToStr(digest);
       } catch (NoSuchAlgorithmException e) {
           e.printStackTrace();
       }
       content = null;
       // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
       System.out.println("tmpStr"+tmpStr);
       System.out.println("signature"+signature.toUpperCase());
       return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
   }


  
   private static String byteToStr(byte[] byteArray) {
       String strDigest = "";
       for (int i = 0; i < byteArray.length; i++) {
           strDigest += byteToHexStr(byteArray[i]);
       }
       return strDigest;
   }


   
   private static String byteToHexStr(byte mByte) {
       char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
       char[] tempArr = new char[2];
       tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
       tempArr[1] = Digit[mByte & 0X0F];
       String s = new String(tempArr);
       return s;
   }
   public static void sort(String a[]) {
       for (int i = 0; i < a.length - 1; i++) {
           for (int j = i + 1; j < a.length; j++) {
               if (a[j].compareTo(a[i]) < 0) {
                   String temp = a[i];
                   a[i] = a[j];
                   a[j] = temp;
               }
           }
       }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值