weichat 开发入门 微信

微信开发入门

 

ngrok 下载

 http://download.csdn.net/download/knight_black_bob/9121979

 

 

ngrok 公网映射
 ngrok -config ngrok.cfg -subdomain curiousby 8080
获得 公网地址
http://curiousby.tunnel.mobi

 

 web.xml

  <servlet>
  	<servlet-name>weichatServlet</servlet-name>
  	<servlet-class>com.curiousby.cn.WeichatServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>weichatServlet</servlet-name> 
  	<url-pattern>/weichat.do</url-pattern>
  </servlet-mapping>

 

 WeichatServlet.java

package com.curiousby.cn;

import java.io.IOException;
import java.io.PrintWriter; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.curiousby.cn.util.WeichatCheckUtils;
 

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年9月20日 下午2:06:42 
 * des:
 */
public class WeichatServlet extends HttpServlet{

	 
	private static final long serialVersionUID = 1L;
	private String signature; //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
	private String timestamp; // 时间戳
	private String nonce; //随机数
	private String echostr; //随机字符串
	
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException { 
		signature = req.getParameter("signature");
		timestamp = req.getParameter("timestamp");
		nonce = req.getParameter("nonce");
		echostr = req.getParameter("echostr");
		System.out.println("signature timestamp  nonce  echostr  " + signature +" " + timestamp +" " + nonce  + " " + echostr ); 
		PrintWriter out = resp.getWriter();
        if (WeichatCheckUtils.checkSignature(signature, timestamp, nonce)) {
            out.print(echostr);
        }

	}
	 
}

 

 

 

CommonUtils.java

 

public class CommonUtils {

	public static String ArrayToString (String [] arr){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			sb.append(arr[i]);
		}
		return sb.toString();
	}
}

 

ShaUtils.java

package com.curiousby.cn.util;

import java.security.MessageDigest;

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年9月20日 下午2:36:04 
 * des:
 */
public class ShaUtils {

	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
		'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	
	
	public static String getSha1(String str){
		if (str == null || str.length() ==0) {
			return null;
		}
		
		try{
			
			MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
			messageDigest.update(str.getBytes("UTF-8"));
			
			byte[] md = messageDigest.digest(); 
			int j  = md.length;
			char [] buf  = new char[j*2];
			int k = 0;
			
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				buf[k++] =HEX_DIGITS[byte0 >>> 4 & 0xf ];
				buf[k++] =HEX_DIGITS[byte0  & 0xf ];
			}
			System.out.println(" before sha1 str :"+ str +"\r\n after sha1  string : "+new String(buf));
			return new String(buf);
		}catch(Exception e){
			return null;
		}
	}
}

 

 WeichatCheckUtils.java

package com.curiousby.cn.util;

import java.util.Arrays;

/**
 * @author baoyou  E-mail:[email protected]
 * @version 创建时间:2015年9月20日 下午2:14:46 
 * des:
 */
public class WeichatCheckUtils {

	
	private static final String TOKEN ="xxxx";
	
	public  static boolean checkSignature(String signature, String timestamp,String nonce){
		String[] str = { TOKEN, timestamp, nonce };
		 Arrays.sort(str); // 字典序排序
		String string = CommonUtils.ArrayToString(str);
		
		String temp = ShaUtils.getSha1(string);
		
		
		 return temp.equals(signature);
	}
	
}

  
weichat 开发入门 微信_第1张图片
 

 

接入成功!

 

 

 

 

 

你可能感兴趣的:(微信,开发入门,weichat)