启用开发者模式--微信公众平台开发(三)

启用开发者模式后,我们的后台便有了和公众号更多的交互的能力,公众号会向我们的后台推送用户的消息、后台拿到用户的消息后,可以实现类似查询天气、关键字回复、聊天之类的等等有趣的功能。

开发者模式在哪里开启呢?


我们需要配置三个值:服务器地址(接收消息的地址)、自定义token、消息加密方式(可选择明文或者密文),暂时地址只支持80端口,其他端口接收不到消息哦。

在开发者首次提交验证申请时,微信服务器将发送GET请求到填写的URL上,并且带上四个参数signature、timestamp、nonce、echostr(开发者配置的token与timestampnonce字典排序后进行sha1加密后的字符串签名、时间戳、随机数、随机字符串),开发者通过对签名(即signature)的效验,来判断此条消息的真实性。此后,每次开发者接收用户消息的时候,微信也都会带上这四个参数访问开发者设置的URL,开发者依然通过对签名的效验判断此条消息的真实性。效验方式与首次提交验证申请一致。

get请求校验请求是否来自微信的方法是:

1. 将token、timestamp、nonce三个参数进行字典排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,若相等,原样返回echostr给微信,若不等,此次请求非微信请求,什么也不做。
后台代码:

  1.         /** 
  2.  * 接收及回复微信消息 
  3.  *  
  4.  * @param token 
  5.  * @param signature 
  6.  * @param timestamp 
  7.  * @param nonce 
  8.  * @param echostr 
  9.  * @param request 
  10.  * @param response 
  11.  * @return 
  12.  * @throws ServletException 
  13.  * @throws IOException 
  14.  */  
  15. @RequestMapping("ansy/weixinMessage")  
  16.  @ResponseBody    
  17. public Callable<Object> weixinMessage(String token, String signature, String timestamp, String nonce,  
  18.         String echostr, HttpServletRequest request, HttpServletResponse response)  
  19.                 throws ServletException, IOException {  
  20.     return new Callable<Object>() {  
  21.         OutData out = new OutData();  
  22.         public Object call() throws Exception {  
  23.             if (request.getMethod().toLowerCase().equals("get")) {  
  24.                 doGet(signature, timestamp, nonce, echostr, response);  
  25.             } else {  
  26.                 doPost(request, response);  
  27.             }  
  28.             out.setCode(1);  
  29.             out.setData(null);  
  30.             out.setMsg("success");  
  31.             return out;  
  32.         }  
  33.     };  
  34.   
  35. }  
  36.   
  37. /** 
  38.  * get方式验证token 
  39.  */  
  40. public void doGet(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response) {  
  41.     //第一个值是服务器配置的token,我们自定义的,和微信的access_token无关  
  42.     String[] ArrTmp = { "liboyi921216liboyisdfsdfsdfsdf", timestamp, nonce };  
  43.     Arrays.sort(ArrTmp);  
  44.     StringBuffer sb = new StringBuffer();  
  45.     for (int i = 0; i < ArrTmp.length; i++) {  
  46.         sb.append(ArrTmp[i]);  
  47.     }  
  48.     String pwd = WeixinMessageDigestUtil.getInstance().encipher(sb.toString());  
  49.     if (pwd.equals(signature)) {  
  50.         if (!"".equals(echostr) && echostr != null) {  
  51.             try {  
  52.                 PrintWriter writer = response.getWriter();  
  53.                 writer.print(echostr);  
  54.                 writer.flush();  
  55.                 writer.close();  
  56.             } catch (IOException e) {  
  57.                 e.printStackTrace();  
  58.             }  
  59.         }  
  60.     }  
  61. }  
  62.   
  63. /** 
  64.  * 处理微信服务器发来的消息 
  65.  */  
  66. public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  67.     // 将请求、响应的编码均设置为UTF-8(防止中文乱码)  
  68.     request.setCharacterEncoding("UTF-8");  
  69.     response.setCharacterEncoding("UTF-8");  
  70.     // 调用核心业务类接收消息、处理消息  
  71.     String respMessage = coreService.processRequest(request);  
  72.     // 响应消息  
  73.     PrintWriter out = response.getWriter();  
  74.     out.print(respMessage);  
  75.     out.flush();  
  76.     out.close();  
  77. }  

注:开启开发者模式后,自动回复消息和自定义菜单会失效。此时菜单需要通过自定义菜单接口去创建,回复消息也只能后台程序里回复。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值