(案例分析)微信服务器的开发环境和接入
2023-09-09 00:00:31 欢乐点

Java微信公众号开发(一:接入平台,验证token)开发环境:

首先,在我们接入微信公众平台之前,我们须要注册微信公众号的开发者帐号微信接单平台,获取开发者ID(AppID),并设置你自己的开发者密码(),服务器地址(URL),令牌(Token),消息加揭秘秘钥()等信息,地址在下边。

前题条件

当我们注册完开发者帐号后,登入,在页面最下方找到’基本配置’,按需求配置就可以了。这儿注意一点的是,自己的开发者密码()一定要保存好,前面好多插口的调用都须要用到。其中上面的一些信息我们后期须要在代码中用到。

代码开发

在代码开发之前,我们应当看一看他腾讯官方给我们的开发文档

在开发文档中的接入手册里,我们能明晰的看见:微信公众号插口必须以或开头微信接单平台,分别支持80端口和443端口。所以,别逞能,8080可不支持,但假如你做转发,那就另说了。

接出来就是紧张剌激的了。

<dependency>
    <groupId>dom4jgroupId>
    <artifactId>dom4jartifactId>
    <version>1.6.1version>
dependency>
<dependency>
    <groupId>com.thoughtworks.xstreamgroupId>
    <artifactId>xstreamartifactId>
    <version>1.4.9version>
dependency>

微信接单平台app_微信接单平台在哪里_微信接单平台

上面的jar包看个人需求。引不引的无所谓,看心情吧,开心才是最重要的了。

首先,我们要晓得,在我们接入微信公众号时,微信服务器会发送一条GET恳求到我们填写的服务器地址URL上,同时在URL上会携带如下参数

参数描述

微信加密签名,结合了开发者填写的token参数和恳求中的参数、nonce参数。

时间戳

nonce

微信接单平台在哪里_微信接单平台app_微信接单平台

随机数

随机字符串

我们须要通过检验对恳求进行校准。若确认这次GET恳求来自微信服务器,正确返回参数内容,则接入生效,成为开发者,否则接入失败。

package com.liu.surprise.until;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
 * @author HB_LIU
 */
public class WeixinCheckoutUtil {
    /**
     * token可以自己进行定义,必须为英文或者是数字,长度为3-32字符,这个token要跟服务器配置中的token一致
     */
    private static String token = "你的微信公众号token";
    /**
     * 校验签名
     * @param signature 签名
     * @param timestamp 时间戳
     * @param nonce 随机数
     * @return 布尔值
     */
    public static boolean checkSignature(String signature,String timestamp,String nonce){
        String checktext = null;
        if (null != signature) {
            //对ToKen,timestamp,nonce 按字典排序
            String[] paramArr = new String[]{token,timestamp,nonce};
            Arrays.sort(paramArr);
            //将排序后的结果拼成一个字符串
            String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                //对接后的字符串进行sha1加密
                byte[] digest = md.digest(content.toString().getBytes());
                checktext = byteToStr(digest);
            } catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }
        }
        //将加密后的字符串与signature进行对比
        return checktext !=null ? checktext.equals(signature.toUpperCase()) : false;
    }
    /**
     * 将字节数组转化我16进制字符串
     * @param byteArrays 字符数组
     * @return 字符串
     */
    private static String byteToStr(byte[] byteArrays){
        String str = "";
        for (int i = 0; i < byteArrays.length; i++) {
            str += byteToHexStr(byteArrays[i]);
        }
        return str;
    }
    /**
     *  将字节转化为十六进制字符串
     * @param myByte 字节
     * @return 字符串
     */
    private static String byteToHexStr(byte myByte) {
        char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char[] tampArr = new char[2];
        tampArr[0] = Digit[(myByte >>> 4) & 0X0F];
        tampArr[1] = Digit[myByte & 0X0F];
        String str = new String(tampArr);
        return str;
    }
}

接出来我们须要传递参数调用里面的方式进行接入,官方文档中早已说明了接入是的恳求为GET恳求,所以,我们须要创建一个拿来调用接入。具体代码如下

package com.liu.surprise.controller;
import com.liu.surprise.service.MessageService;
import com.liu.surprise.until.WeixinCheckoutUtil;
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.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/**
 * @author HB_LIU
 */
@RestController
@RequestMapping("/wxPublic")
public class WeixinCheckController {
    @Resource
    MessageService messageService;
    private static final Logger LOGGER = LoggerFactory.getLogger(WeixinCheckController.class);
    @RequestMapping(method = RequestMethod.GET)
    public String checkToken(HttpServletRequest request, HttpServletResponse response) {
        // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
            if (WeixinCheckoutUtil.checkSignature(signature, timestamp, nonce)) {
                LOGGER.info("微信加密签名:" + signature + ";时间戳:" + timestamp + ";随机数:" + nonce);
                out.print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            out = null;
        }
        return null;
    }
}

测试

微信公众号接入、验证token的代码部份到这就结束了,接出来我们须要启动项目,通过微信公众平台插口测试工具测试代码。并且由于我们的代码如今是跑在我们本地,微信未能发送恳求到我们本地,这就须要用到一个叫外网穿透的工具了,网上有好多,这个你们就可以自由发挥了。我用的是ngrok.下载好后,我们通过命令行cd到ngrok的储存目录,输入下边的命令。

免责声明:部分文章信息来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性,如本站文章和转稿涉及版权等问题,请作者在及时联系本站,我们会尽快为您处理。

欢乐点

留言咨询

×