In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how java implements background access to Wechat public platform". The content in the article is simple, clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how java achieves background access to Wechat public platform".
I. preliminary preparation
The project is built with maven and can be directly imported into eclipse. The version of jdk is 1.8.0,111. These two items can be modified according to the actual needs, and finally war can be released to the server.
II. Actual combat exercises
Before operation, read the official documents first, there is a train of thought process as a whole, official address reference
1. Set it to developer mode:
After logging in to the backend of Wechat public platform, click "Features"-"Advanced Features"-"Development Mode" to enter the development mode. If the public platform displays "not yet a developer", click "become a developer".
two。 Fill in the server configuration:
Click "Development"-"basic configuration"-"fill in server configuration" as shown in figure 1 ~ 3
Figure 1
Figure 2
Figure 3
3. Sample code:
SHA1.java
Package com.dqiang.demo; public class SHA1 {private final int [] abcde = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0}; / / Abstract data storage array private int [] digestInt = new int [5]; / / temporary data storage array private int [] tmpData = new int [80] / / calculate sha-1 digest private int process_input_bytes (byte [] bytedata) {/ / pilot constant System.arraycopy (abcde, 0, digestInt, 0, abcde.length); / / format input byte array, complement 10 and length data byte [] newbyte = byteArrayFormatData (bytedata); / / get the number of data units calculated by data digest int MCount = newbyte.length / 64 / / loop the summary calculation for (int pos = 0; pos) for each data unit
< MCount; pos++) { // 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中 for (int j = 0; j < 16; j++) { tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4)); } // 摘要计算函数 encrypt(); } return 20; } // 格式化输入字节数组格式 private byte[] byteArrayFormatData(byte[] bytedata) { // 补0数量 int zeros = 0; // 补位后总位数 int size = 0; // 原始数据长度 int n = bytedata.length; // 模64后的剩余位数 int m = n % 64; // 计算添加0的个数以及添加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; } // 补位后生成的新数组内容 byte[] newbyte = new byte[size]; // 复制数组的前面部分 System.arraycopy(bytedata, 0, newbyte, 0, n); // 获得数组Append数据元素的位置 int l = n; // 补1操作 newbyte[l++] = (byte) 0x80; // 补0操作 for (int i = 0; i < zeros; i++) { newbyte[l++] = (byte) 0x00; } // 计算数据长度,补数据长度位共8字节,长整型 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 [lumped +] = h2; newbyte [lumped +] = h3; newbyte [lumped +] = h4 Newbyte [lumped +] = h5; newbyte [lumped +] = h6; newbyte [lumped +] = h7; newbyte [lumped +] = h7; newbyte [lumped +] = 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 > > (32-y);} / / Unit abstract calculation function private void encrypt () {for (int I = 16; I > 8); byteData [I + 3] = (byte) intValue } / / convert bytes to hexadecimal strings private static String byteToHexString (byte ib) {char [] Digit = {'0,1,' 2, 3, 4, 5, 6, 7, 8, 9, 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;} / / convert a byte array to a hexadecimal string private static String byteArrayToHexString (byte [] bytearray) {String strDigest = "; for (int I = 0; I < bytearray.length; itrees +) {strDigest + = byteToHexString (bytearay [I]);} return strDigest } / / calculate the sha-1 digest and return the corresponding byte array public byte [] getDigestOfBytes (byte [] byteData) {process_input_bytes (byteData); byte [] digest = new byte [20]; for (int I = 0; I < digestInt.length; isummary +) {intToByteArray (digestInt [I], digest, I * 4);} return digest } / / calculate the sha-1 digest and return the corresponding hexadecimal string 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 / * for example, * URL (server address) http://weixin.xxxx.com/weChatJavaTokenValidate/wechatToken * Token (token) tokenChat * * / private String TOKEN = "tokenChat"; / / defining token according to the actual situation is the same as basic configuration / filling in server configuration Token (token) @ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / Wechat encryption signature String signature = request.getParameter ("signature") / / Random string String echostr = request.getParameter ("echostr"); / / timestamp String timestamp = request.getParameter ("timestamp"); / / Random number String nonce = request.getParameter ("nonce"); String [] str = {TOKEN, timestamp, nonce}; / / Arrays.sort (str); String bigStr = str [0] + str [1] + str [2] / / SHA1 encryption String digest = new SHA1 () .getDigestOfString (bigStr.getBytes ()) .toLowerCase (); / / confirm that the request comes from Wechat if (digest.equals (signature)) {response.getWriter () .print (echostr);}
3. Source code download: weChatJavaTokenValidate
Thank you for your reading, the above is the content of "how java realizes the background access of Wechat public platform". After the study of this article, I believe you have a deeper understanding of how java realizes the background access of Wechat public platform, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.