黄色草逼视频_久久夜夜视频_亚洲国产成人久久午夜_三级在线播放

網站改版|WAP網站制作|域名注冊|虛擬主機|服務器|海微商|海微信|海微通| 無障礙| 24小時服務電話:13807590485
海南網站建設海南網站制作海口網站建設三亞網站建設儋州網站建設五指山網站建設文昌網站建設瓊海網站建設萬寧網站建設東方網站建設定安網站建設 網站首頁網站首頁 網站建設網站建設 微信開發微信開發 網站推廣網站推廣 海南網站建設公司,海南網站開發制作公司,海南網頁設計公司,海南小程序開發公司,海南微信公眾號開發公司,海南網絡公司,海南世紀華聯海南世紀華聯 網站超市網站超市 客戶案例客戶案例 網站模板網站模板 關于我們關于我們
  • 微信開發
  • 持之以恒
  • 網站設計制作
  • 中立五年回報客戶
無障礙
微信開發
微信系統開發 微信開發功能 公眾號基礎教程 開發技術資訊 公眾號推廣營銷 客戶案例
聯系我們
QQ服務群:28519571 工作時間:86-0898-31568080 傳真號碼:86-0898-31568085 24小時服務:0138-07590485
 您現在的位置: 首頁 >> 微信開發 >> 開發技術資訊 開發技術資訊
微信公共平臺驗證接口JAVA實現
世紀華聯 | 2018-01-26 23:53:52 | 閱讀:13662
看到微信的公共平臺接入文檔接口驗證的例子是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. }  
復制代碼


海南網站建設|海南網站制作|海口網站建設|三亞網站建設|儋州網站建設|五指山網站建設|文昌網站建設|瓊海網站建設|萬寧網站建設|東方網站建設|定安網站建設|屯昌網站建設|澄邁網站建設|臨高網站建設|白沙網站建設|昌江網站建設|樂東網站建設|陵水網站建設|保亭網站建設|瓊中網站建設|海口精英網|三亞精英網|文昌精英網|瓊海精英網|陵水精英網|儋州精英網|萬寧精英網|澄邁精英網|海微通
合作伙伴 企業發展 企業文化 聯系我們 在線訂購 網站地圖 返回首頁手機版
海口世紀華聯科技有限公司2025版權所有 24小時服務熱線:13807590485   歡迎來電咨詢
地址:海南省.海口市.海甸二東路環惠大廈6樓(南寶路明都大廈107#) 公司電話:0898-31568080 31568060 QQ:85398489
全國合作聯盟分布:海南海口 四川成都 湖北武漢 湖南長沙 安徽合肥 廣東深圳 山西太原 西藏拉薩
技術服務: E_mail:server@www.0118yy.com 服務訂購:E_mail:server@www.0118yy.com 在線客服邀請 瓊ICP備10201086號-15
推廣關鍵字:海南網站建設公司,海南網站制作公司,海南網站開發公司,海南微信開發公司,海南微信公眾號開發公司,海南微信公眾號代運營公司,海南模板網站制作公司,海南網頁設計公司,海南網絡公司