微信公眾號伺服器配置token驗證
阿新 • • 發佈:2019-01-03
pom中引入jar:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.1.0</version> </dependency>
token驗證方法:
/** * 將微信公眾號傳送來的加密後的訊息體解密 * @return * @throws Exception */ @GetMapping(produces = "text/plain;charset=utf-8",value = "/msg") public static String msgEncrypt( @RequestParam(name = "signature",required = false) String signature, @RequestParam(name = "timestamp",required = false) String timestamp, @RequestParam(name = "nonce", required = false) String nonce, @RequestParam(name = "echostr", required = false) String echostr ) throws Exception{ System.out.print("進入token驗證"); System.out.print("signature:"+signature); String token = "token"; //字典序排序 String[] strs = new String[]{token,timestamp,nonce}; Arrays.sort(strs); System.out.print("加密後:"+SHA1.gen(strs)); try { //判斷加密後的字串與signature字串是否一致 if( SHA1.gen(strs).equals(signature)){ //一致返回rechostr return echostr; } else { //不一致返回error return "error"; } } catch (Exception var5) { return "error"; } }