标签:eal source ima end 支持 ref -o etc tac
1 package com.wechat; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.apache.commons.io.IOUtils; 12 13 import com.wechat.utility.WeChatUtil; 14 15 public class WechatConService extends HttpServlet 16 { 17 18 private static final long serialVersionUID = 1L; 19 /** 20 * 确认请求来自微信服务器 21 */ 22 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 23 { 24 25 // 微信加密签名 26 String signature = request.getParameter("signature"); 27 // 时间戳 28 String timestamp = request.getParameter("timestamp"); 29 // 随机数 30 String nonce = request.getParameter("nonce"); 31 // 随机字符串 32 String echostr = request.getParameter("echostr"); 33 34 PrintWriter out = response.getWriter(); 35 36 System.out.println(signature); 37 System.out.println(timestamp); 38 System.out.println(nonce); 39 System.out.println(echostr); 40 41 // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败 42 if (WeChatUtil.checkSignature(signature, timestamp, nonce)) 43 { 44 out.print(echostr); 45 } 46 47 out.close(); 48 out = null; 49 } 50 51 /** 52 * 处理微信服务器发来的消息 53 */ 54 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 55 { 56 // TODO 消息的接收、处理、响应 57 System.out.println("接口调用了一次"); 58 59 String ret = IOUtils.toString(request.getInputStream(), "utf-8").toString(); 60 System.out.println(ret); 61 62 WeChatMsgBL tWeChatMsgBL = new WeChatMsgBL(); 63 String result = tWeChatMsgBL.dealWechatMsg(ret); 64 65 response.setCharacterEncoding("UTF-8"); 66 67 // 返回文本消息,将处理结果返回 68 PrintWriter out = response.getWriter(); 69 70 System.out.println(result); 71 72 out.print(result); 73 74 out.close(); 75 76 } 77 78 }
2、配置web.xml
1 <!-- 微信公众号测试接口 add by Damon start--> 2 <servlet> 3 <servlet-name>WechatconService</servlet-name> 4 <servlet-class>com.wechat.WechatConService</servlet-class> 5 </servlet> 6 7 <servlet-mapping> 8 <servlet-name>WechatconService</servlet-name> 9 <url-pattern>/Servlet/WechatconService</url-pattern> 10 </servlet-mapping> 11 12 <!-- 微信公众号测试接口 add by Damon end-->
1 /** 2 * 方法名:checkSignature</br> 详述:验证签名</br> 开发人员:Damon</br> 创建时间:2017-07-11 3 * </br> 4 * @param signature 5 * @param timestamp 6 * @param nonce 7 * @return 8 * @throws 9 */ 10 public static boolean checkSignature(String signature, String timestamp, String nonce) 11 { 12 // 1.将token、timestamp、nonce三个参数进行字典序排序 13 String[] arr = new String[] 14 { token, timestamp, nonce }; 15 Arrays.sort(arr); 16 17 // 2. 将三个参数字符串拼接成一个字符串进行sha1加密 18 StringBuilder content = new StringBuilder(); 19 for (int i = 0; i < arr.length; i++) 20 { 21 content.append(arr[i]); 22 } 23 MessageDigest md = null; 24 String tmpStr = null; 25 try 26 { 27 md = MessageDigest.getInstance("SHA-1"); 28 // 将三个参数字符串拼接成一个字符串进行sha1加密 29 byte[] digest = md.digest(content.toString().getBytes()); 30 tmpStr = byteToStr(digest); 31 } 32 catch (NoSuchAlgorithmException e) 33 { 34 e.printStackTrace(); 35 } 36 37 content = null; 38 // 3.将sha1加密后的字符串可与signature对比,标识该请求来源于微信 39 return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false; 40 } 41 42 /** 43 * 方法名:byteToStr</br> 详述:将字节数组转换为十六进制字符串</br> 开发人员:Damon </br> 44 * 创建时间:2017-07-11 </br> 45 * @param byteArray 46 * @return 47 * @throws 48 */ 49 private static String byteToStr(byte[] byteArray) 50 { 51 String strDigest = ""; 52 for (int i = 0; i < byteArray.length; i++) 53 { 54 strDigest += byteToHexStr(byteArray[i]); 55 } 56 return strDigest; 57 } 58 59 /** 60 * 方法名:byteToHexStr</br> 详述:将字节转换为十六进制字符串</br> 开发人员:Damon</br> 61 * 创建时间:2017-07-11 </br> 62 * @param mByte 63 * @return 64 * @throws 65 */ 66 private static String byteToHexStr(byte mByte) 67 { 68 char[] Digit = 69 { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ }; 70 char[] tempArr = new char[2]; 71 tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; 72 tempArr[1] = Digit[mByte & 0X0F]; 73 String s = new String(tempArr); 74 return s; 75 }
标签:eal source ima end 支持 ref -o etc tac
原文地址:http://www.cnblogs.com/cooldamon/p/7208612.html