微信公衆號開發筆記-驗證token

開發


該項目須要創建java web項目,使用tomcat做爲web容器。(有條件的可使用本身買的服務器和域名來進行操做)html

咱們先去微信公衆號申請一個公衆號:
java

申請完成以後咱們找到開發下的基本配置web

而後找到進行基本配置,咱們須要一個url地址來驗證,這裏的地址必須要是外網,Token是咱們任意填寫而後在程序後端填寫一致的token進行驗證算法

 

 

 

 

 

沒有url的小夥伴們咱們利用natapp實現內網穿透(也可使用ngrok,我的推薦natapp 後端

百度natapp:https://natapp.cn/註冊一個帳號而後點擊購買隧道有免費的api

 

而後咱們點擊配置:tomcat

 

 綁定域名咱們本身任意填寫,地址端口如圖(tomcat端口是多少就是多少,地址不變)服務器

 

 

 接着在個人隧道里面找到隧道複製authtoken。微信

經過在DOS窗口運行natapp -authtoken *******(*******表明你在natapp官網他給你的authtoken)指令獲得以下圖,啓動tomcat以後此時咱們就能夠經過tree.natapp1.cc訪問tomcat80端口了微信開發

 

 注意:這裏我域名爲tree.natapp1.cc,小夥伴們須要根據本身設置的域名來訪問

 

 咱們啓動tomcat後能經過本身設置的域名訪問到tomcat的8080端口就說明成功了!

好了,咱們開始建立項目;

注意:Wechat 本次須要的jar包有servlet-api.jar(由於我本身開發的過程當中沒有這個包是後面導入的因此提醒一下)

首先建立weixinServlet文件(在建立時咱們能夠修改訪問地址如圖)

咱們將URL mapping中的內容選中,而後點擊Edit,改爲任意本身想訪問的就好了。

經過微信開發文檔咱們知道微信後臺會經過GET方式發送signature,timestamp,nonce,echostr。

咱們將token,timestamp,nonce進行字典序排序sha1加密後與signature進行對比。

經過則能夠進行開發,其中涉及到的sha1加密算法及weixinServlet代碼以下

 這些能夠在羅召勇老師的視頻中有詳細的講解,連接我放在下面。

 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletInputStream;
 8 import javax.servlet.ServletOutputStream;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import service.WxService;
14 /**
15  * Servlet implementation class weixinServlet
16  */
17 @WebServlet("/wx")
18 public class weixinServlet extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20 
21     /**
22      * Default constructor. 
23      */
24     public weixinServlet() {
25         // TODO Auto-generated constructor stub
26     }
27     /**
28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
29      */
30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31     
32     String signature=request.getParameter("signature");
33     String timestamp=request.getParameter("timestamp");
34     String nonce=request.getParameter("nonce");
35     String echostr=request.getParameter("echostr");
36     
37     if(WxService.check(timestamp,nonce,signature)) {
38         System.out.println("接入成功");    
39         PrintWriter out=response.getWriter();
40         out.print(echostr);
41         out.flush();
42         out.close();
43     }
44     else {
45         System.out.println("失敗");
46     }
47     }
48 
49     /**
50      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
51      */
52     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
53     ServletInputStream is=request.getInputStream();
54     byte[] b=new byte[1024];
55     int len;
56     StringBuilder sb=new StringBuilder();
57     while((len=is.read(b))!=-1) {
58         sb.append(new String(b,0,len));
59     }
60     System.out.println(sb.toString());
61     }
62 }

WxService.java以下

package service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class WxService {
	private static final String TOKEN="bestsegho0914";
   public static boolean check(String timestamp,String nonce,String signature) {

	   String[] strs=new String[] {TOKEN,timestamp,nonce};
	   Arrays.sort(strs);	   
	   String str=strs[0]+strs[1]+strs[2]; 
	   String mysig=sha1(str);
	   System.out.println(mysig);
	   System.out.println(signature);
	   return mysig.equals(signature);   
   }
    
   private static String sha1(String src) {
	   try {
		MessageDigest md=MessageDigest.getInstance("sha1");
	   byte[] digest=md.digest(src.getBytes());
		
	   char[] chars= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
	  StringBuilder sb=new StringBuilder();
	  for (byte b:digest) {
		sb.append(chars[(b>>4)&15]);
		sb.append(chars[b&15]);
	       }
	   return sb.toString();
	   } catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;   
   }
   
}

完成代碼後咱們點擊運行 run as ->run on server,在tomcat上啓動

而後回到咱們微信公衆號的後臺,點擊配置信息的修改按鈕 點擊提交 這裏就會顯示配置成功

 

 

 配置成功如圖

 

 

 這時咱們就能夠開始進行公衆號我的開發了。

本人也是初次進行微信的java開發,分享一些我正在看的資料但願能幫到想要學習微信開發的小夥伴們

羅召勇java開發:https://www.bilibili.com/video/av35042298

微信開發幫助文檔:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html

微信公衆號測試號申請入口:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

ps:全部文章用於我的學習,僅表明我的理解。

相關文章
相關標籤/搜索