微信公共平台驗證介面JAVA實現

2021-08-27 15:55:58 字數 3556 閱讀 5251

response.setcontenttype("text/html");

printwriter out = response.getwriter();

string signature = request.getparameter("signature");

string timestamp = request.getparameter("timestamp");

string nonce = request.getparameter("nonce");

string echostr = request.getparameter("echostr");

string resignature = null;

try ;

arrays.sort(str);

string bigstr = str[0] + str[1] + str[2];

resignature = new sha1().getdigestofstring(bigstr.getbytes())

.tolowercase();

} catch (exception e)

if (null != resignature && resignature.equals(signature)) else

out.flush();

out.close();

邏輯**就這些,就是按照文件上說的來:

1. 將token、timestamp、nonce三個引數進行字典序排序

2. 將三個引數字串拼接成乙個字串進行sha1加密

上面**中有乙個工具類sha1,就是實現了乙個加密解密演算法,網上也有,這裡貼一下**。

package duanqing.test.servlet.tools;

public class sha1 ;

// 摘要資料儲存陣列

private int digestint = new int[5];

// 計算過程中的臨時資料儲存陣列

private int tmpdata = new int[80];

// 計算sha-1摘要

private int process_input_bytes(byte bytedata)

// 摘要計算函式

encrypt();

} return 20;

} // 格式化輸入位元組陣列格式

private byte bytearrayformatdata(byte bytedata) else if (m == 56) else

// 補位後生成的新陣列內容

byte newbyte = new byte[size];

// 複製陣列的前面部分

system.arraycopy(bytedata, 0, newbyte, 0, n);

int l = n;

// 補1操作

newbyte[l++] = (byte) 0x80;

// 補0操作

for (int i = 0; i < zeros; i++)

// 計算資料長度,補資料長度位共8位元組,長整型

long n = (long) n * 8;

byte h8 = (byte) (n & 0xff);

byte h7 = (byte) ((n >> 8) & 0xff);

byte h6 = (byte) ((n >> 16) & 0xff);

byte h5 = (byte) ((n >> 24) & 0xff);

byte h4 = (byte) ((n >> 32) & 0xff);

byte h3 = (byte) ((n >> 40) & 0xff);

byte h2 = (byte) ((n >> 48) & 0xff);

byte h1 = (byte) (n >> 56);

newbyte[l++] = h1;

newbyte[l++] = h2;

newbyte[l++] = h3;

newbyte[l++] = h4;

newbyte[l++] = h5;

newbyte[l++] = h6;

newbyte[l++] = h7;

newbyte[l++] = h8;

return newbyte;

} private int f1(int x, int y, int z)

private int f2(int x, int y, int z)

private int f3(int x, int y, int z)

private int f4(int x, int y)

// 單元摘要計算函式

private void encrypt()

int tmpabcde = new int[5];

for (int i1 = 0; i1 < tmpabcde.length; i1++)

for (int j = 0; j <= 19; j++)

for (int k = 20; k <= 39; k++)

for (int l = 40; l <= 59; l++)

for (int m = 60; m <= 79; m++)

for (int i2 = 0; i2 < tmpabcde.length; i2++)

for (int n = 0; n < tmpdata.length; n++)

} // 4位元組陣列轉換為整數

private int bytearraytoint(byte bytedata, int i)

// 整數轉換為4位元組陣列

private void inttobytearray(int intvalue, byte bytedata, int i)

// 將位元組轉換為十六進製制字串

private static string bytetohexstring(byte ib) ;

char ob = new char[2];

ob[0] = digit[(ib >>> 4) & 0x0f];

ob[1] = digit[ib & 0x0f];

string s = new string(ob);

return s;

} // 將位元組陣列轉換為十六進製制字串

private static string bytearraytohexstring(byte bytearray)

return strdigest;

} // 計算sha-1摘要,返回相應的位元組陣列

public byte getdigestofbytes(byte bytedata)

return digest;

} // 計算sha-1摘要,返回相應的十六進製制字串

public string getdigestofstring(byte bytedata)

public static void main(string args)

}

工具**摘自網路!

微信開發 one 微信平台驗證

2.在我們驗證期間可以用兩種方式進行處理 echostr protected void page load object sender,eventargs e private void valid 2 官方驗證,就需要進行加密匹配 private void valid 將token timestam...

(一)驗證微信公眾平台

第一步,進入驗證頁面 提交到伺服器上的報文為 這時候,需要將你的token,timestamp,和nonce三個字串的值,按照字母排序進行拼接,然後進行sha1,所得出的結果,應該與signature一致,最後,將echostr返回給伺服器。廢話少說了,上 function validatetoke...

微信公眾平台開發第一步(Java) 介面驗證問題

正式申請還需要,身份驗證,300元費用,作為想學習的人,可以先申請乙個公眾平台測試號。2.遇到問題1 開發第一步介面測試不通過 租用虛擬主機空間,有公網ip,正確安裝jdk與tomcat,再經過兩天的努力,看了無數網路文章,對驗證專案做了各種修改後,驗證成功。解決方案 1.重新安裝伺服器上的tomc...