微信开发——开启开发者模式

微信开发的第一步--就是开启开发者模式。

1.后台写好相关的验证信息

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.io.PrintWriter;
 4 import java.util.Arrays;
 5 import java.util.Map;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import org.apache.commons.io.IOUtils;
13 import org.apache.log4j.Logger;
14 
15 import com.gzcc.admin.util.AddSHA1;
16 import com.gzcc.admin.vo.WeiXinData;
17 import com.gzcc.client.platform.message.util.MessageUtil;
18 
19 /**
20  * 核心请求处理类
21  */
22 public class WeixinServlet extends HttpServlet {
23 
24     private static Logger logger = Logger.getLogger(WeixinServlet.class);
25 
26 
27     /**
28      * 确认请求来自微信服务器
29      */
30     public void doGet(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32         // 微信加密签名
33         String signature = request.getParameter("signature");
34         // 时间戳
35         String timestamp = request.getParameter("timestamp");
36         // 随机数
37         String nonce = request.getParameter("nonce");
38         // 随机字符串
39         String echostr = request.getParameter("echostr");
40 
41         logger.info("[WeiXin] checkSignature signature=" + signature
42                 + " timestamp=" + timestamp + " nonce=" + nonce + " echostr="
43                 + echostr);
44 
45         String token = WeiXinData.getToken();
46         String[] args = new String[] { token, timestamp, nonce };
47         Arrays.sort(args);
48 
49         String tmpStr = "";
50         for (String arg : args) {
51             tmpStr += arg;
52         }
53         tmpStr = AddSHA1.SHA1(tmpStr);
54 
55         logger.info("[WeiXin] checkSignature tmpStr=" + tmpStr + " signature="
56                 + signature);
57 
58         PrintWriter out = response.getWriter();
59         if (tmpStr.equals(signature)) {
60             out.print(echostr);
61         }
62         out.close();
63         out = null;
64     }
65 
66     /**
67      * 处理微信服务器发来的消息
68      */
69     public void doPost(HttpServletRequest request, HttpServletResponse response) {
70 
71     }
72 
73 }

2.到微信官方后台填写相关的URL和token

3.点击提交即可开启开发者模式

 

总结:开发者模式开启比较简单。就是根据微信端发过来的signature,timestamp,nonce,echostr的四个参数,并通过将token、timestamp、nonce三个参数进行字典序排         序,将三个参数字符串拼接成一个字符串进行sha1加密,开发者获得加密后的字符串可与signature对比,相同则信息来自微信服务器。开发者模式就可以成功开启。后面带来       企业的加密的开发者回调。

posted @ 2016-04-11 18:01  LawlietL  阅读(1638)  评论(0编辑  收藏  举报