基于springboot+mybatis的微信公众号开发第二篇-验证与消息推送的基本设置

来源:互联网 发布:cfve软件 编辑:程序博客网 时间:2024/05/18 15:51

——因为博主是一个刚刚出来实习的小菜鸟,所以博文中不详细或者错误的地方可以提出来,大家一起进步。

一、根据微信公众号开发文档编写的验证方法,写在util包中。

1、首先在util包下创建SignUtil类

package com.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Arrays;public class SignUtil {    // 与接口配置信息中的Token要一致    private static String token = "weixinmp";    /**     * 验证签名     *     * @param signature     * @param timestamp     * @param nonce     * @return     */    public static boolean checkSignature(String signature, String timestamp, String nonce) {        String[] arr = new String[] { token, timestamp, nonce };        // 将token、timestamp、nonce三个参数进行字典序排序        Arrays.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对比,标识该请求来源于微信        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;    }    /**     * 将字节数组转换为十六进制字符串     *     * @param byteArray     * @return     */    private static String byteToStr(byte[] byteArray) {        String strDigest = "";        for (int i = 0; i < byteArray.length; i++) {            strDigest += byteToHexStr(byteArray[i]);        }        return strDigest;    }    /**     * 将字节转换为十六进制字符串     *     * @param mByte     * @return     */    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;    }}

2、在controller包下创建核心控制类CoreController

package com.controller;import com.util.SignUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("")public class CoreController {    //增加日志    private static Logger log = LoggerFactory.getLogger(CoreController.class);    //验证是否来自微信服务器的消息    @RequestMapping(value = "",method = RequestMethod.GET)    public String checkSignature(@RequestParam(name = "signature" ,required = false) String signature  ,                                 @RequestParam(name = "nonce",required = false) String  nonce ,                                 @RequestParam(name = "timestamp",required = false) String  timestamp ,                                 @RequestParam(name = "echostr",required = false) String  echostr){        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败        if (SignUtil.checkSignature(signature, timestamp, nonce)) {            log.info("接入成功");            return echostr;        }        log.error("接入失败");        return "";    }  }

注意

2、接下来设置微信公众号开发的url和token

url必须是外网地址(我是通过ngrok来创建一个外网地址映射到本地地址,可以搜索下ngrok的使用方法)。

设置完成后,run项目,然后再点击公众号的接口配置信息的提交,成功的话就显示配置成功,否则显示失败。(有时候微信大姨妈,第一次总是配置失败<不知道是ngrok的问题不>,要点击2次才配置成功。)

这里写图片描述

二、消息的处理

1、在service包中创建一个core包,在core包中创建接口类CoreService和实现类CoreServiceImpl(这里你也可以不用创建core包,直接在service包中建这两个类,我这是为了后期的维护便利而已。)

CoreService.java
package com.service.core;import javax.servlet.http.HttpServletRequest;/** * Created by Administrator on 2016/11/8. */public interface CoreService {    public  String processRequest(HttpServletRequest request) ;}
CoreServiceImpl.java
/** * 核心服务类 */@Service("coreService")    public class CoreServiceImpl implements CoreService {    private static Logger log = LoggerFactory.getLogger(CoreServiceImpl.class);    /**     * 处理微信发来的请求(包括事件的推送)     *     * @param request     * @return     */    public  String processRequest(HttpServletRequest request) {    //暂时对消息不作处理      return "";    }

3、再在Corecontroller类中加入、对消息的处理方法,我给出完整类

package com.controller;import com.service.core.CoreService;import com.util.SignUtil;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;@RestController@RequestMapping("")public class CoreController {    @Autowired    private CoreService coreService;    //增加日志    private static Logger log = LoggerFactory.getLogger(CoreController.class);    //验证是否来自微信服务器的消息    @RequestMapping(value = "",method = RequestMethod.GET)    public String checkSignature(@RequestParam(name = "signature" ,required = false) String signature  ,                                 @RequestParam(name = "nonce",required = false) String  nonce ,                                 @RequestParam(name = "timestamp",required = false) String  timestamp ,                                 @RequestParam(name = "echostr",required = false) String  echostr){        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败        if (SignUtil.checkSignature(signature, timestamp, nonce)) {            log.info("接入成功");            return echostr;        }        log.error("接入失败");        return "";    }    // 调用核心业务类接收消息、处理消息跟推送消息    @RequestMapping(value = "",method = RequestMethod.POST)    public  String post(HttpServletRequest req){        String respMessage = coreService.processRequest(req);        return respMessage;    }  }

由于时间问题,暂时写到这里,下一篇讲对消息的接收与回复。

0 0
原创粉丝点击