微信驗證開發者伺服器接口
微信驗證開發者伺服器接口
微信公衆号開發(一)微信驗證開發者伺服器接口 - 如圖所示,開發者可填寫自己伺服器的驗證token的接口位址,以及自定義的token(部落客申請的測試号,使用natapp來進行内網穿透)
- 目的:幫助微信伺服器和開發者伺服器互相識别,以防惡意攻擊
- 流程圖如下(不知道部落格園怎麼顯示md流程圖,有知道的仁兄告知):(取自微信公衆平台技術文檔)
```flow
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
```
@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() : "";
}