微信公眾號開發(一)微信驗證開發者服務器接口
阿新 • • 發佈:2017-12-31
mage down charset 流程圖 ringbuf image put 不知道 als
微信驗證開發者服務器接口
微信驗證開發者服務器接口
- 如圖所示,開發者可填寫自己服務器的驗證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() : ""; }
微信公眾號開發(一)微信驗證開發者服務器接口