微信公衆號開發(一)微信驗證開發者服務器接口

微信驗證開發者服務器接口

微信驗證開發者服務器接口

  • 如圖所示,開發者可填寫本身服務器的驗證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() : "";
    }
相關文章
相關標籤/搜索