网站改版|WAP网站制作|域名注册|虚拟主机|服务器|海微商|海微信|海微通| 无障碍| 24小时服务电话:13807590485
海南网站建设海南网站制作海口网站建设三亚网站建设儋州网站建设五指山网站建设文昌网站建设琼海网站建设万宁网站建设东方网站建设定安网站建设 网站首页网站首页 网站建设网站建设 微信开发微信开发 网站推广网站推广 东方网站建设,东方网站开发制作,东方网页设计,东方小程序开发,东方微信公众号开发,东方网络公司,东方世纪华联东方世纪华联 网站超市网站超市 客户案例客户案例 网站模板网站模板 关于我们关于我们
  • 微信开发
  • 持之以恒
  • 网站设计制作
  • 中立五年回报客户
无障碍
微信开发
微信系统开发 微信开发功能 公众号基础教程 开发技术资讯 公众号推广营销 客户案例
联系我们
QQ服务群:28519571 工作时间:86-0898-31568080 传真号码:86-0898-31568085 24小时服务:0138-07590485
 您现在的位置: 首页 >> 微信开发 >> 开发技术资讯 开发技术资讯
微信公共平台验证接口JAVA实现
世纪华联 | 2018-01-26 23:53:52 | 阅读:10542
看到微信的公共平台接入文档接口验证的例子是PHP写的,对于很多不是做php的人来说有点麻烦,虽然编程思想是相同的,逻辑也很简单,但是一种语言有一种语言的语法规范,其实代码的编写还是差距挺大的。这里写一下JAVA版接口验证的实现。
  1. response.setContentType("text/html");  
  2.         PrintWriter out = response.getWriter();  
  3.         String signature = request.getParameter("signature");  
  4.         String timestamp = request.getParameter("timestamp");  
  5.         String nonce = request.getParameter("nonce");  
  6.         String echostr = request.getParameter("echostr");  
  7.         String reSignature = null;  
  8.         try {  
  9.             String[] str = { TOKEN, timestamp, nonce };  
  10.             Arrays.sort(str);  
  11.             String bigStr = str[0] + str[1] + str[2];  
  12.             reSignature = new SHA1().getDigestOfString(bigStr.getBytes())  
  13.                     .toLowerCase();  
  14.         } catch (Exception e) {  
  15.             e.printStackTrace();  
  16.         }  
  17.         if (null != reSignature && reSignature.equals(signature)) {  
  18.             //请求来自微信  
  19.             out.print(echostr);  
  20.         } else {  
  21.             out.print("error request! the request is not from weixin server");  
  22.         }  
  23.         out.flush();  
  24.         out.close();
复制代码

逻辑代码就这些,就是按照文档上说的来:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信


上面代码中有一个工具类SHA1,就是实现了一个加密解密算法,网上也有,这里贴一下代码。
  1. package duanqing.test.servlet.tools;  
  2.   
  3. public class SHA1 {  
  4.     private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe,  
  5.             0x10325476, 0xc3d2e1f0 };  
  6.     // 摘要数据存储数组  
  7.     private int[] digestInt = new int[5];  
  8.     // 计算过程中的临时数据存储数组  
  9.     private int[] tmpData = new int[80];  
  10.   
  11.     // 计算sha-1摘要  
  12.     private int process_input_bytes(byte[] bytedata) {  
  13.         // 初试化常量  
  14.         System.arraycopy(abcde, 0, digestInt, 0, abcde.length);  
  15.         // 格式化输入字节数组,补10及长度数据  
  16.         byte[] newbyte = byteArrayFormatData(bytedata);  
  17.         // 获取数据摘要计算的数据单元个数  
  18.         int MCount = newbyte.length / 64;  
  19.         // 循环对每个数据单元进行摘要计算  
  20.         for (int pos = 0; pos < MCount; pos++) {  
  21.             // 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中  
  22.             for (int j = 0; j < 16; j++) {  
  23.                 tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));  
  24.             }  
  25.             // 摘要计算函数  
  26.             encrypt();  
  27.         }  
  28.         return 20;  
  29.     }  
  30.   
  31.     // 格式化输入字节数组格式  
  32.     private byte[] byteArrayFormatData(byte[] bytedata) {  
  33.         // 补0数量  
  34.         int zeros = 0;  
  35.         // 补位后总位数  
  36.         int size = 0;  
  37.         // 原始数据长度  
  38.         int n = bytedata.length;  
  39.         // 模64后的剩余位数  
  40.         int m = n % 64;  
  41.         // 计算添加0的个数以及添加10后的总长度  
  42.         if (m < 56) {  
  43.             zeros = 55 - m;  
  44.             size = n - m + 64;  
  45.         } else if (m == 56) {  
  46.             zeros = 63;  
  47.             size = n + 8 + 64;  
  48.         } else {  
  49.             zeros = 63 - m + 56;  
  50.             size = (n + 64) - m + 64;  
  51.         }  
  52.         // 补位后生成的新数组内容  
  53.         byte[] newbyte = new byte[size];  
  54.         // 复制数组的前面部分  
  55.         System.arraycopy(bytedata, 0, newbyte, 0, n);  
  56.         // 获得数组Append数据元素的位置  
  57.         int l = n;  
  58.         // 补1操作  
  59.         newbyte[l++] = (byte) 0x80;  
  60.         // 补0操作  
  61.         for (int i = 0; i < zeros; i++) {  
  62.             newbyte[l++] = (byte) 0x00;  
  63.         }  
  64.         // 计算数据长度,补数据长度位共8字节,长整型  
  65.         long N = (long) n * 8;  
  66.         byte h8 = (byte) (N & 0xFF);  
  67.         byte h7 = (byte) ((N >> 8) & 0xFF);  
  68.         byte h6 = (byte) ((N >> 16) & 0xFF);  
  69.         byte h5 = (byte) ((N >> 24) & 0xFF);  
  70.         byte h4 = (byte) ((N >> 32) & 0xFF);  
  71.         byte h3 = (byte) ((N >> 40) & 0xFF);  
  72.         byte h2 = (byte) ((N >> 48) & 0xFF);  
  73.         byte h1 = (byte) (N >> 56);  
  74.         newbyte[l++] = h1;  
  75.         newbyte[l++] = h2;  
  76.         newbyte[l++] = h3;  
  77.         newbyte[l++] = h4;  
  78.         newbyte[l++] = h5;  
  79.         newbyte[l++] = h6;  
  80.         newbyte[l++] = h7;  
  81.         newbyte[l++] = h8;  
  82.         return newbyte;  
  83.     }  
  84.   
  85.     private int f1(int x, int y, int z) {  
  86.         return (x & y) | (~x & z);  
  87.     }  
  88.   
  89.     private int f2(int x, int y, int z) {  
  90.         return x ^ y ^ z;  
  91.     }  
  92.   
  93.     private int f3(int x, int y, int z) {  
  94.         return (x & y) | (x & z) | (y & z);  
  95.     }  
  96.   
  97.     private int f4(int x, int y) {  
  98.         return (x << y) | x >>> (32 - y);  
  99.     }  
  100.   
  101.     // 单元摘要计算函数  
  102.     private void encrypt() {  
  103.         for (int i = 16; i <= 79; i++) {  
  104.             tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14]  
  105.                     ^ tmpData[i - 16], 1);  
  106.         }  
  107.         int[] tmpabcde = new int[5];  
  108.         for (int i1 = 0; i1 < tmpabcde.length; i1++) {  
  109.             tmpabcde[i1] = digestInt[i1];  
  110.         }  
  111.         for (int j = 0; j <= 19; j++) {  
  112.             int tmp = f4(tmpabcde[0], 5)  
  113.                     + f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  114.                     + tmpData[j] + 0x5a827999;  
  115.             tmpabcde[4] = tmpabcde[3];  
  116.             tmpabcde[3] = tmpabcde[2];  
  117.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  118.             tmpabcde[1] = tmpabcde[0];  
  119.             tmpabcde[0] = tmp;  
  120.         }  
  121.         for (int k = 20; k <= 39; k++) {  
  122.             int tmp = f4(tmpabcde[0], 5)  
  123.                     + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  124.                     + tmpData[k] + 0x6ed9eba1;  
  125.             tmpabcde[4] = tmpabcde[3];  
  126.             tmpabcde[3] = tmpabcde[2];  
  127.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  128.             tmpabcde[1] = tmpabcde[0];  
  129.             tmpabcde[0] = tmp;  
  130.         }  
  131.         for (int l = 40; l <= 59; l++) {  
  132.             int tmp = f4(tmpabcde[0], 5)  
  133.                     + f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  134.                     + tmpData[l] + 0x8f1bbcdc;  
  135.             tmpabcde[4] = tmpabcde[3];  
  136.             tmpabcde[3] = tmpabcde[2];  
  137.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  138.             tmpabcde[1] = tmpabcde[0];  
  139.             tmpabcde[0] = tmp;  
  140.         }  
  141.         for (int m = 60; m <= 79; m++) {  
  142.             int tmp = f4(tmpabcde[0], 5)  
  143.                     + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  144.                     + tmpData[m] + 0xca62c1d6;  
  145.             tmpabcde[4] = tmpabcde[3];  
  146.             tmpabcde[3] = tmpabcde[2];  
  147.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  148.             tmpabcde[1] = tmpabcde[0];  
  149.             tmpabcde[0] = tmp;  
  150.         }  
  151.         for (int i2 = 0; i2 < tmpabcde.length; i2++) {  
  152.             digestInt[i2] = digestInt[i2] + tmpabcde[i2];  
  153.         }  
  154.         for (int n = 0; n < tmpData.length; n++) {  
  155.             tmpData[n] = 0;  
  156.         }  
  157.     }  
  158.   
  159.     // 4字节数组转换为整数  
  160.     private int byteArrayToInt(byte[] bytedata, int i) {  
  161.         return ((bytedata[i] & 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16)  
  162.                 | ((bytedata[i + 2] & 0xff) << 8) | (bytedata[i + 3] & 0xff);  
  163.     }  
  164.   
  165.     // 整数转换为4字节数组  
  166.     private void intToByteArray(int intValue, byte[] byteData, int i) {  
  167.         byteData[i] = (byte) (intValue >>> 24);  
  168.         byteData[i + 1] = (byte) (intValue >>> 16);  
  169.         byteData[i + 2] = (byte) (intValue >>> 8);  
  170.         byteData[i + 3] = (byte) intValue;  
  171.     }  
  172.   
  173.     // 将字节转换为十六进制字符串  
  174.     private static String byteToHexString(byte ib) {  
  175.         char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',  
  176.                 'B', 'C', 'D', 'E', 'F' };  
  177.         char[] ob = new char[2];  
  178.         ob[0] = Digit[(ib >>> 4) & 0X0F];  
  179.         ob[1] = Digit[ib & 0X0F];  
  180.         String s = new String(ob);  
  181.         return s;  
  182.     }  
  183.   
  184.     // 将字节数组转换为十六进制字符串  
  185.     private static String byteArrayToHexString(byte[] bytearray) {  
  186.         String strDigest = "";  
  187.         for (int i = 0; i < bytearray.length; i++) {  
  188.             strDigest += byteToHexString(bytearray[i]);  
  189.         }  
  190.         return strDigest;  
  191.     }  
  192.   
  193.     // 计算sha-1摘要,返回相应的字节数组  
  194.     public byte[] getDigestOfBytes(byte[] byteData) {  
  195.         process_input_bytes(byteData);  
  196.         byte[] digest = new byte[20];  
  197.         for (int i = 0; i < digestInt.length; i++) {  
  198.             intToByteArray(digestInt[i], digest, i * 4);  
  199.         }  
  200.         return digest;  
  201.     }  
  202.   
  203.     // 计算sha-1摘要,返回相应的十六进制字符串  
  204.     public String getDigestOfString(byte[] byteData) {  
  205.         return byteArrayToHexString(getDigestOfBytes(byteData));  
  206.     }  
  207.   
  208.     public static void main(String[] args) {  
  209.         String data = "123456";  
  210.         System.out.println(data);  
  211.         String digest = new SHA1().getDigestOfString(data.getBytes()).toLowerCase();  
  212.         System.out.println(digest);  
  213.   
  214.         // System.out.println( ToMD5.convertSHA1(data).toUpperCase());  
  215.     }  
  216. }  
复制代码


海南网站建设|海南网站制作|海口网站建设|三亚网站建设|儋州网站建设|五指山网站建设|文昌网站建设|琼海网站建设|万宁网站建设|东方网站建设|定安网站建设|屯昌网站建设|澄迈网站建设|临高网站建设|白沙网站建设|昌江网站建设|乐东网站建设|陵水网站建设|保亭网站建设|琼中网站建设|海口精英网|三亚精英网|文昌精英网|琼海精英网|陵水精英网|儋州精英网|万宁精英网|澄迈精英网|海微通
合作伙伴 企业发展 企业文化 联系我们 在线订购 网站地图 返回首页手机版
海口世纪华联科技有限公司2024版权所有 24小时服务热线:13807590485   欢迎来电咨询
地址:海南省.海口市.海甸二东路环惠大厦6楼(南宝路明都大厦107#) 公司电话:0898-31568080 31568060 QQ:85398489
全国合作联盟分布:海南海口 四川成都 湖北武汉 湖南长沙 安徽合肥 广东深圳 山西太原 西藏拉萨
技术服务: E_mail:server@hnzlweb.com 服务订购:E_mail:server@hnzlweb.com 在线客服邀请 琼ICP备10201086号-15
推广关键字:东方网站建设,东方网站制作,东方网站开发,东方微信开发,东方微信公众号开发,东方微信公众号代运营,东方模板网站制作,东方网页设计,东方网络公司