微信公众号开发指南:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

从文档可知步骤如下:

1、填写服务器配置

2、验证服务器地址的有效性

3、依据接口文档实现业务逻辑

一.填写服务器配置 登录后在首页------找基础配置点击之后如下

二.验证服务器地址的有效性

上面信息填写完毕后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带参数如下表所示: 通过检验signature对请求进行校验: 1)将token、timestamp、nonce三个参数进行字典序排序 2)将三个参数字符串拼接成一个字符串进行sha1加密 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 首先来搞一个sha1的工具类

	import java.security.MessageDigest;

	public class SHA1 {
		 private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',  
							'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};  


				private static String getFormattedText(byte[] bytes) {  
					 int len = bytes.length;  
					 StringBuilder buf = new StringBuilder(len * 2);  
					 // 把密文转换成十六进制的字符串形式  
					 for (int j = 0; j < len; j++) {  
					 buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);  
					 buf.append(HEX_DIGITS[bytes[j] & 0x0f]);  
					 }  
					 return buf.toString();  
				}  

				public static String encode(String str) {  
					 if (str == null) {  
					 return null;  
					 }  
					 try {  
					 MessageDigest messageDigest = MessageDigest.getInstance("SHA1");  
					 messageDigest.update(str.getBytes());  
					 return getFormattedText(messageDigest.digest());  
					 } catch (Exception e) {  
					 throw new RuntimeException(e);  
					 }  
				}  
	}
``` 然后在来校验的工具类
	public class CheckUtil {
						private static final String token = "";   //这里的token就是配置中的token令牌
						public static boolean checkSignature(String signature,String timestamp,String nonce){
								String[] str = new String[]{token,timestamp,nonce};
								//排序
								Arrays.sort(str);
								//拼接字符串
								StringBuffer buffer = new StringBuffer();
								for(int i =0 ;i<str.length;i++){
										buffer.append(str[i]);
								}
								//进行sha1加密
								String temp = SHA1.encode(buffer.toString());
								//与微信提供的signature进行匹对
								return signature.equals(temp);
						}
				}
  接下来,就是验证啦,搞个controller来验证起来

@RequestMapping(value = "/test",method=RequestMethod.GET)
		public void get(HttpServletRequest request,HttpServletResponse response){
				System.out.println("success");
				String signature = request.getParameter("signature");
				String timestamp = request.getParameter("timestamp");
				String nonce = request.getParameter("nonce");
				String echostr = request.getParameter("echostr");
				PrintWriter out = null;
				try {
							out = response.getWriter();
						if(CheckUtil.checkSignature(signature, timestamp, nonce)){
								out.write(echostr);
						}
				} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
				}finally{
						out.close();
				}

		}

		@RequestMapping(value = "/test",method=RequestMethod.POST)
		public void post(HttpServletRequest request,HttpServletResponse response){
		//这里主要处理以后的消息,事件等等。

		}