微信公众号<入门指引> 验证token

由于我也是初次接触微信公众号,而<入门指引>是以python为例子,特总结出以Java为例的对接方式:

1、搭建服务:简单的SpringBoot Web项目即可。

@RestController
@RequestMapping("/wechat")
public class WeChatController {

    @GetMapping("/wx")
    public String handle() {
        return "hello, this is handle view.";
    }
}

测试地址:http://xx.xx.27.225/wechat/wx

 

   

2、公众号申请,个人可以申请一个订阅号,很简单。

3、公众号==>开发==>基本配置(服务器配置)

WeChtController.java 再写一个方法,接收 signature、timestamp、nonce、echostr参数

    @GetMapping()
    public String signatureVerify(String signature, String timestamp, String nonce, String echostr) {

        String token = "abc123";
        boolean b = WeChatUtils.checkSignature(signature, token, timestamp, nonce);

        return b ? echostr : null;
    }

重启项目后,在微信公众号基本配置这边url填写项目访问地址,token和项目中的token一致,其他的根据情况填写即可,最后提交则会访问项目,项目中会根据传入的参数验证signature是否正确(项目部署在云服务器上)。

 

 

完整代码如下:

WeChatControllerjava

package com.study.yq.controller;

import com.study.yq.util.WeChatUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 进行微信公众号签名验证的demo项目
 *
 * @author (yangqiang)
 * @date 2021/04/15 15:01
 */
@RestController
@RequestMapping("/wechat")
public class WeChatController {

    @GetMapping("/wx")
    public String handle() {
        return "hello, this is handle view.";
    }

    @GetMapping()
    public String signatureVerify(String signature, String timestamp, String nonce, String echostr) {

        String token = "abc123";
        boolean b = WeChatUtils.checkSignature(signature, token, timestamp, nonce);

        return b ? echostr : null;
    }

}

WeChatUtils.java

package com.study.yq.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Formatter;

/**
 * 微信工具类
 *
 * @author (yangqiang)
 * @date 2021/04/13 9:35
 */
public class WeChatUtils {

    /**
     * 验证消息的确来自微信服务器
     *
     * @param msgSignature 微信加密签名
     * @param token        开发者填写的token参数
     * @param timeStamp    时间戳
     * @param nonce        随机数
     * @return
     * @throws
     */
    public static boolean checkSignature(String msgSignature, String token, String timeStamp, String nonce) {

        // 1.将token、timestamp、nonce三个参数进行字典序排序
        String[] arr = new String[]{token, timeStamp, nonce};
        Arrays.sort(arr);

        // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
        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 =byteToHex(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // 3.将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tmpStr != null ? tmpStr.equals(msgSignature.toUpperCase()) : false;
    }


    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

}

 

仅为学习微信公众号时的个人记录,借鉴需谨慎!

2021.04.19

 

posted @ 2021-04-19 10:59  从零|再出发  阅读(754)  评论(0编辑  收藏  举报