Java 微信公众号开发--- 接入微信

开发微信公众号在没有正式的公众平台账号时,我们可以使用测试平台账号———

测试平台申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

进入之后我们会看见 此时 appID、appsecret都有了,url是我们成为开发者与微信进行的一次握手配置(url其实就是我们项目中你controller的访问地址,token是我们自己填写的,可在后台进行判断的),这里的url可以用ngrok来做映射,这样开发起来比较方便,ngrok配置(点击查看)ngrok下载地址(点击下载)

我们可以在开发者文档中看见

这里查看详细配置;期间我们要说道微信会给我们的url通过get方式传递4个参数

下面我们来看一下controller怎么编写

  1. package com.website.wechat.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.security.MessageDigest;  
  5. import java.util.ArrayList;  
  6. import java.util.Collections;  
  7. import java.util.List;  
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.web.bind.annotation.RequestMapping;  
  14. import org.springframework.web.bind.annotation.RequestMethod;  
  15. import org.springframework.web.bind.annotation.ResponseBody;  
  16.   
  17. @Controller  
  18. @RequestMapping(value="weixin")  
  19. public class WeiXinController {  
  20.       
  21.     private static final char[] HEX_DIGITS = { '0''1''2''3''4''5',  
  22.         '6''7''8''9''a''b''c''d''e''f' };  
  23.   
  24.     @RequestMapping(value="getWeiXinMethod",method=RequestMethod.GET)  
  25.     @ResponseBody  
  26.     public void getWeiXinMethod(HttpServletRequest request, HttpServletResponse response) throws IOException{  
  27.         boolean validate = validate(request);  
  28.         if (validate) {  
  29.             response.getWriter().write(request.getParameter("echostr"));  
  30.             response.getWriter().close();  
  31.         }  
  32.           
  33.     }  
  34.       
  35.     private boolean validate(HttpServletRequest req) throws IOException {  
  36.         String signature = req.getParameter("signature");//微信加密签名  
  37.         String timestamp = req.getParameter("timestamp");//时间戳  
  38.         String nonce = req.getParameter("nonce");//随机数  
  39.         List<String> list = new ArrayList<String>();  
  40.         list.add("chenchen");  
  41.         list.add(timestamp);  
  42.         list.add(nonce);  
  43.         Collections.sort(list);//字典排序  
  44.         String s = "";  
  45.         for (int i = 0; i < list.size(); i++) {  
  46.             s += (String) list.get(i);  
  47.         }  
  48.         if (encode("SHA1", s).equalsIgnoreCase(signature)) {  
  49.             return true;  
  50.         } else {  
  51.             return false;  
  52.         }  
  53.     }  
  54.   
  55.     public static String encode(String algorithm, String str) {  
  56.         if (str == null) {  
  57.             return null;  
  58.         }  
  59.         try {  
  60.             //Java自带的加密类  
  61.             MessageDigest messageDigest = MessageDigest.getInstance(algorithm);  
  62.             //转为byte  
  63.             messageDigest.update(str.getBytes());  
  64.             return getFormattedText(messageDigest.digest());  
  65.         } catch (Exception e) {  
  66.             throw new RuntimeException(e);  
  67.         }  
  68.     }  
  69.   
  70.     private static String getFormattedText(byte[] bytes) {  
  71.         int len = bytes.length;  
  72.         StringBuilder buf = new StringBuilder(len * 2);  
  73.         // 把密文转换成十六进制的字符串形式  
  74.         for (int j = 0; j < len; j++) {  
  75.             buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);  
  76.             buf.append(HEX_DIGITS[bytes[j] & 0x0f]);  
  77.         }  
  78.         return buf.toString();  
  79.     }  
  80. }  
此时验证和调用已经没问题,成功介入微信,成为一名开发者

猜你喜欢

转载自www.cnblogs.com/jpfss/p/9035340.html