5-2 开发模式接入

首先,登录 微信公众平台  https://mp.weixin.qq.com/

我登录进入了

配置好eclipse

新建一个Java web项目

新建 WeixinServlet

继承 HttpServlet,如果继承不了,需要到tomcat的lib包下面,拷贝如下两个包到项目的lib下面

如图,创建好了WeixinServlet

输入 dog 可以快捷出doGet方法。

目录结构

/Weixin/src/com/imooc/servlet/WeixinServlet.java

package com.imooc.servlet;

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.imooc.util.CheckUtil;

public class WeixinServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String signature = req.getParameter("signature");
		String timestamp = req.getParameter("timestamp");
		String nonce = req.getParameter("nonce");
		String echostr = req.getParameter("echostr"); //随机字符串
		
		PrintWriter out = resp.getWriter();
		if(CheckUtil.checkSignature(signature, timestamp, nonce)){
			out.print(echostr);
		}
				
	}
}

/Weixin/src/com/imooc/util/CheckUtil.java

package com.imooc.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class CheckUtil {

	private static final String token = "imooc";
	public static boolean checkSignature(String signature,String timestamp,String nonce){
		
		String[] arr = new String[]{token,timestamp,nonce};
		
		//排序
		Arrays.sort(arr);
		
		//生成字符串
		StringBuffer content = new StringBuffer();
		for(int i=0;i < arr.length; i++){
			content.append(arr[i]);
		}
		
		// sha1加密
		String temp = getSha1(content.toString());
		
		return temp.equals(signature);
		
	}
	
	public static String getSha1(String str){
		if(str == null || str.length() == 0){
			return null;
		}
		
		char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
		
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("SHA1");

			mdTemp.update(str.getBytes("UTF-8"));
			
			byte[] md = mdTemp.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++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0  & 0xf];
			}
			return new String(buf);
		}catch(Exception e){
			return null;
		}
	}
}

/Weixin/WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Weixin</display-name>
  
  <servlet>
  	<servlet-name>weixinServlet</servlet-name>
  	<servlet-class>com.imooc.servlet.WeixinServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>weixinServlet</servlet-name>
  	<url-pattern>/wx.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/huanglianggu/article/details/81585036
5-2