1. 程式人生 > >微信公眾號 伺服器配置

微信公眾號 伺服器配置

在公眾號基本配置–伺服器配置中 啟用伺服器 並修改配置
伺服器配置
配置類:

public class MyCheck {
	 public static final String  token = "lixinchuxing"; //開發者自行定義Tooken  
	  
	    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>  
	    * 開發人員:souvc </br>  
	    * 建立時間:2015-9-29  </br>  
	    * @param byteArray  
	    * @return  
	    * @throws  
	     */  
	    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>  
	    * 開發人員:souvc</br>  
	    * 建立時間:2015-9-29  </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;  
	    }  
}

驗證類:

public class MyServer extends BaseAction{

	 private static final long serialVersionUID = 4323197796926899691L;  
	 //myserver.action?method=CheckSign
	 public void CheckSign() throws IOException {	
	  	
	 String appid = Params.appid;
	 //APPID
	 String secret = Params.secret;
	 //secret
         String signature = getRequest().getParameter("signature");  
         // 時間戳  
         String timestamp = getRequest().getParameter("timestamp");  
         // 隨機數  
         String nonce = getRequest().getParameter("nonce");  
         // 隨機字串  
         String echostr = getRequest().getParameter("echostr");  
         System.out.println("sign:"+signature+";time:"+timestamp+";nonce:"+nonce+";echostr"+echostr);  
         PrintWriter out = getResponse().getWriter();  
         
         // 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗  
         if (MyCheck.checkSignature(signature, timestamp, nonce)) {  
             out.print(echostr);  
         }  
     }