微信公眾號和伺服器連線
阿新 • • 發佈:2018-12-06
package com.ztsy.zbt.controller;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("weChatController")
public class WeChatController extends ZBTBaseController{
// 與介面配置資訊中的Token要一致
private static String token = "zheshiyige";
/**
* 確認訊息來自微信伺服器
* @param request
* @return
*/
@ResponseBody
@RequestMapping("weChatPassWay")
String weChatPassWay(HttpServletRequest request) {
// 微信加密簽名
String signature = request.getParameter("signature");
// 時間戳
String timestamp = request.getParameter("timestamp");
// 隨機數
String nonce = request.getParameter("nonce");
// 隨機字串
String echostr = request.getParameter("echostr");
// 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗
if (checkSignature(signature, timestamp, nonce)) {
System.out.print("echostr="+echostr);
return echostr;
}else{
return "fail";
}
}
/**
* 方法名:checkSignature</br>
* 詳述:驗證簽名</br>
*/
public static boolean checkSignature(String signature, String timestamp,String nonce) {
// 1.將token、timestamp、nonce三個引數進行字典序排序
String[] arr = new String[] { token, timestamp, nonce };
Arrays.sort(arr);
// 2. 將三個引數字串拼接成一個字串進行sha1加密
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 將三個引數字串拼接成一個字串進行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 3.將sha1加密後的字串可與signature對比,標識該請求來源於微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
/**
* 方法名:byteToStr</br>
* 詳述:將位元組陣列轉換為十六進位制字串</br>
* @param byteArray
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 方法名:byteToHexStr</br>
* 詳述:將位元組轉換為十六進位制字串</br>
* @param mByte
* @return
* @throws
*/
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B', 'C', 'D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("weChatController")
public class WeChatController extends ZBTBaseController{
// 與介面配置資訊中的Token要一致
private static String token = "zheshiyige";
/**
* 確認訊息來自微信伺服器
* @param request
* @return
*/
@ResponseBody
@RequestMapping("weChatPassWay")
String weChatPassWay(HttpServletRequest request) {
// 微信加密簽名
String signature = request.getParameter("signature");
// 時間戳
String timestamp = request.getParameter("timestamp");
// 隨機數
String nonce = request.getParameter("nonce");
// 隨機字串
String echostr = request.getParameter("echostr");
// 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗
if (checkSignature(signature, timestamp, nonce)) {
System.out.print("echostr="+echostr);
return echostr;
}else{
return "fail";
}
}
/**
* 方法名:checkSignature</br>
* 詳述:驗證簽名</br>
*/
public static boolean checkSignature(String signature, String timestamp,String nonce) {
// 1.將token、timestamp、nonce三個引數進行字典序排序
String[] arr = new String[] { token, timestamp, nonce };
Arrays.sort(arr);
// 2. 將三個引數字串拼接成一個字串進行sha1加密
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 將三個引數字串拼接成一個字串進行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 3.將sha1加密後的字串可與signature對比,標識該請求來源於微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
/**
* 方法名:byteToStr</br>
* 詳述:將位元組陣列轉換為十六進位制字串</br>
* @param byteArray
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 方法名:byteToHexStr</br>
* 詳述:將位元組轉換為十六進位制字串</br>
* @param mByte
* @return
* @throws
*/
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B', 'C', 'D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
}