码迷,mamicode.com
首页 > 微信 > 详细

微信公众号开发(一)微信验证开发者服务器接口

时间:2017-12-31 16:03:18      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:tin   mes   logs   hash   ping   enc   lan   end   out   

微信验证开发者服务器接口

微信验证开发者服务器接口

技术分享图片

  • 如图所示,开发者可填写自己服务器的验证token的接口地址,以及自定义的token(博主申请的测试号,使用natapp来进行内网穿透)
  • 目的:帮助微信服务器和开发者服务器互相识别,以防恶意攻击
  • 流程图如下(不知道博客园怎么显示md流程图,有知道的仁兄告知):(取自微信公众平台技术文档)


    st=>start: 开启服务
    ipop1=>inputoutput: 接收到数据【不确定是谁发来的】
    op1=>operation: 尝试提取出signature字段,timestamp字段,nonce字段,echostr字段
    cd1=>condition: 字段均提取成功?
    op2=>operation: token赋值为基本配置中的信息
    op3=>operation: token,timestamp,nonce字段排序得到字符串list
    op4=>operation: 哈希算法加密list得到hashcode
    cd2=>condition: hashcode == signature?
    op5=>operation: 确定该数据源是微信后台
    ipop2=>inputoutput: 把echostr返回给微信后台,供微信后台认证Token
    ed=>end: 继续其他服务
    op6=>operation: 确定该数据源不是微信后台
    ipop3=>inputoutput: 不处理
    st->ipop1->op1->cd1->op2->op3->op4->cd2->op5->ipop2->ed
    cd1(yes)->op2
    cd1(no)->op6->ipop3->ed
    cd2(yes)->op5
    cd2(no)->op6


  • java代码实现如下:

    @GetMapping("/getToken")
    @ResponseBody
    public String getToken(TokenDTO tokenDTO, HttpServletResponse response){
        if ((StringUtils.isBlank(tokenDTO.getSignature()) || StringUtils.isBlank(tokenDTO.getTimestamp()) || StringUtils.isBlank(tokenDTO.getNonce()) || StringUtils.isBlank(tokenDTO.getEchostr()))) {
            return "";
        }
        String[] arr = new String[]{tokenDTO.getTimestamp(), WeixinConstant.token, tokenDTO.getNonce()};
        Arrays.sort(arr);
        StringBuffer sb = new StringBuffer();
        sb.append(arr[0]).append(arr[1]).append(arr[2]);
        String hash = null;
        try {
            hash = new String(Hex.encodeHex(MessageDigest.getInstance("SHA-1").
                    digest(sb.toString().getBytes(Constant.charset))));
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return (StringUtils.isNoneBlank(hash) && hash.equals(tokenDTO.getSignature()))
                ? tokenDTO.getEchostr() : "";
    }

微信公众号开发(一)微信验证开发者服务器接口

标签:tin   mes   logs   hash   ping   enc   lan   end   out   

原文地址:https://www.cnblogs.com/tswhq/p/8157795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!