您好,登錄后才能下訂單哦!
這篇文章主要講解了“java怎么實現(xiàn)微信公眾平臺后臺接入”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“java怎么實現(xiàn)微信公眾平臺后臺接入”吧!
一、前期準(zhǔn)備
項目是用maven構(gòu)建,直接導(dǎo)入到eclipse即可,jdk的版本為1.8.0_111 ,這兩項都可以根據(jù)實際需求進(jìn)行修改,最終達(dá)成war發(fā)布到服務(wù)器中即可。
二、實戰(zhàn)演練
操作前,先閱讀一下官方的文檔,先整體有個思路流程,官方地址參考
1.設(shè)置成開發(fā)者模式:
登錄微信公眾平臺后臺后,點「功能」-「高級功能」-「開發(fā)模式」,進(jìn)入開發(fā)模式,如果公眾平臺顯示「尚未成為開發(fā)者」,就點擊「成為開發(fā)者」
2.填寫服務(wù)器配置:
點「開發(fā)」-「基本配置」-「填寫服務(wù)器配置」如圖1~如3
圖1
圖2
圖3
3.示例代碼:
SHA1.java
package com.dqiang.demo; public class SHA1 { private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 }; // 摘要數(shù)據(jù)存儲數(shù)組 private int[] digestInt = new int[5]; // 計算過程中的臨時數(shù)據(jù)存儲數(shù)組 private int[] tmpData = new int[80]; // 計算sha-1摘要 private int process_input_bytes(byte[] bytedata) { // 初試化常量 System.arraycopy(abcde, 0, digestInt, 0, abcde.length); // 格式化輸入字節(jié)數(shù)組,補10及長度數(shù)據(jù) byte[] newbyte = byteArrayFormatData(bytedata); // 獲取數(shù)據(jù)摘要計算的數(shù)據(jù)單元個數(shù) int MCount = newbyte.length / 64; // 循環(huán)對每個數(shù)據(jù)單元進(jìn)行摘要計算 for (int pos = 0; pos < MCount; pos++) { // 將每個單元的數(shù)據(jù)轉(zhuǎn)換成16個整型數(shù)據(jù),并保存到tmpData的前16個數(shù)組元素中 for (int j = 0; j < 16; j++) { tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4)); } // 摘要計算函數(shù) encrypt(); } return 20; } // 格式化輸入字節(jié)數(shù)組格式 private byte[] byteArrayFormatData(byte[] bytedata) { // 補0數(shù)量 int zeros = 0; // 補位后總位數(shù) int size = 0; // 原始數(shù)據(jù)長度 int n = bytedata.length; // 模64后的剩余位數(shù) int m = n % 64; // 計算添加0的個數(shù)以及添加10后的總長度 if (m < 56) { zeros = 55 - m; size = n - m + 64; } else if (m == 56) { zeros = 63; size = n + 8 + 64; } else { zeros = 63 - m + 56; size = (n + 64) - m + 64; } // 補位后生成的新數(shù)組內(nèi)容 byte[] newbyte = new byte[size]; // 復(fù)制數(shù)組的前面部分 System.arraycopy(bytedata, 0, newbyte, 0, n); // 獲得數(shù)組Append數(shù)據(jù)元素的位置 int l = n; // 補1操作 newbyte[l++] = (byte) 0x80; // 補0操作 for (int i = 0; i < zeros; i++) { newbyte[l++] = (byte) 0x00; } // 計算數(shù)據(jù)長度,補數(shù)據(jù)長度位共8字節(jié),長整型 long N = (long) n * 8; byte h8 = (byte) (N & 0xFF); byte h7 = (byte) ((N >> 8) & 0xFF); byte h7 = (byte) ((N >> 16) & 0xFF); byte h6 = (byte) ((N >> 24) & 0xFF); byte h5 = (byte) ((N >> 32) & 0xFF); byte h4 = (byte) ((N >> 40) & 0xFF); byte h3 = (byte) ((N >> 48) & 0xFF); byte h2 = (byte) (N >> 56); newbyte[l++] = h2; newbyte[l++] = h3; newbyte[l++] = h4; newbyte[l++] = h5; newbyte[l++] = h6; newbyte[l++] = h7; newbyte[l++] = h7; newbyte[l++] = h8; return newbyte; } private int f1(int x, int y, int z) { return (x & y) | (~x & z); } private int f2(int x, int y, int z) { return x ^ y ^ z; } private int f3(int x, int y, int z) { return (x & y) | (x & z) | (y & z); } private int f4(int x, int y) { return (x << y) | x >>> (32 - y); } // 單元摘要計算函數(shù) private void encrypt() { for (int i = 16; i <= 79; i++) { tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14] ^ tmpData[i - 16], 1); } int[] tmpabcde = new int[5]; for (int i1 = 0; i1 < tmpabcde.length; i1++) { tmpabcde[i1] = digestInt[i1]; } for (int j = 0; j <= 19; j++) { int tmp = f4(tmpabcde[0], 5) + f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[j] + 0x5a827999; tmpabcde[4] = tmpabcde[3]; tmpabcde[3] = tmpabcde[2]; tmpabcde[2] = f4(tmpabcde[1], 30); tmpabcde[1] = tmpabcde[0]; tmpabcde[0] = tmp; } for (int k = 20; k <= 39; k++) { int tmp = f4(tmpabcde[0], 5) + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[k] + 0x6ed9eba1; tmpabcde[4] = tmpabcde[3]; tmpabcde[3] = tmpabcde[2]; tmpabcde[2] = f4(tmpabcde[1], 30); tmpabcde[1] = tmpabcde[0]; tmpabcde[0] = tmp; } for (int l = 40; l <= 59; l++) { int tmp = f4(tmpabcde[0], 5) + f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[l] + 0x8f1bbcdc; tmpabcde[4] = tmpabcde[3]; tmpabcde[3] = tmpabcde[2]; tmpabcde[2] = f4(tmpabcde[1], 30); tmpabcde[1] = tmpabcde[0]; tmpabcde[0] = tmp; } for (int m = 60; m <= 79; m++) { int tmp = f4(tmpabcde[0], 5) + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[m] + 0xca62c1d6; tmpabcde[4] = tmpabcde[3]; tmpabcde[3] = tmpabcde[2]; tmpabcde[2] = f4(tmpabcde[1], 30); tmpabcde[1] = tmpabcde[0]; tmpabcde[0] = tmp; } for (int i2 = 0; i2 < tmpabcde.length; i2++) { digestInt[i2] = digestInt[i2] + tmpabcde[i2]; } for (int n = 0; n < tmpData.length; n++) { tmpData[n] = 0; } } // 4字節(jié)數(shù)組轉(zhuǎn)換為整數(shù) private int byteArrayToInt(byte[] bytedata, int i) { return ((bytedata[i] & 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16) | ((bytedata[i + 2] & 0xff) << 8) | (bytedata[i + 3] & 0xff); } // 整數(shù)轉(zhuǎn)換為4字節(jié)數(shù)組 private void intToByteArray(int intValue, byte[] byteData, int i) { byteData[i] = (byte) (intValue >>> 24); byteData[i + 1] = (byte) (intValue >>> 16); byteData[i + 2] = (byte) (intValue >>> 8); byteData[i + 3] = (byte) intValue; } // 將字節(jié)轉(zhuǎn)換為十六進(jìn)制字符串 private static String byteToHexString(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; } // 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串 private static String byteArrayToHexString(byte[] bytearray) { String strDigest = ""; for (int i = 0; i < bytearray.length; i++) { strDigest += byteToHexString(bytearray[i]); } return strDigest; } // 計算sha-1摘要,返回相應(yīng)的字節(jié)數(shù)組 public byte[] getDigestOfBytes(byte[] byteData) { process_input_bytes(byteData); byte[] digest = new byte[20]; for (int i = 0; i < digestInt.length; i++) { intToByteArray(digestInt[i], digest, i * 4); } return digest; } // 計算sha-1摘要,返回相應(yīng)的十六進(jìn)制字符串 public String getDigestOfString(byte[] byteData) { return byteArrayToHexString(getDigestOfBytes(byteData)); } public static void main(String[] args) { String data = "tokenDemo"; System.out.println(data); String digest = new SHA1().getDigestOfString(data.getBytes()); System.out.println(digest); } }
weChatJavaTokenValidate.java
package com.dqiang.demo; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author StemQ * @version v1.0 * Blog:http://blog.csdn.net/stemq * Web:www.dqiang.com */ public class weChatJavaTokenValidate extends HttpServlet { private static final long serialVersionUID = -6761982938477193120L; /* 例如 * URL(服務(wù)器地址) http://weixin.xxxx.com/weChatJavaTokenValidate/wechatToken * Token(令牌) tokenChat * */ private String TOKEN = "tokenChat"; //根據(jù)實際情況自己定義token與基本配置/填寫服務(wù)器配置Token(令牌)相同 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 微信加密簽名 String signature = request.getParameter("signature"); // 隨機(jī)字符串 String echostr = request.getParameter("echostr"); // 時間戳 String timestamp = request.getParameter("timestamp"); // 隨機(jī)數(shù) String nonce = request.getParameter("nonce"); String[] str = { TOKEN, timestamp, nonce }; // 字典序排序 Arrays.sort(str); String bigStr = str[0] + str[1] + str[2]; // SHA1加密 String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase(); // 確認(rèn)請求來至微信 if (digest.equals(signature)) { response.getWriter().print(echostr); } } }
3.源碼下載:weChatJavaTokenValidate
感謝各位的閱讀,以上就是“java怎么實現(xiàn)微信公眾平臺后臺接入”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對java怎么實現(xiàn)微信公眾平臺后臺接入這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。