微信公众号Java开发-笔记02【开发接入准备、开发接入】

学习视频网址:哔哩哔哩网站 微信公众号开发-Java版

  1. 【P01-P02】微信公众号Java开发-笔记01【微信公众号介绍、开发环境搭建】
  2. 【P03-P04】微信公众号Java开发-笔记02【开发接入准备、开发接入】
  3. 【P00-P00】微信公众号Java开发-笔记03【】
  4. 【P00-P00】微信公众号Java开发-笔记04【】
  5. 【P00-P00】微信公众号Java开发-笔记05【】

目录

P3 1.3 开发接入准备 19:08

基本配置

服务号 开发文档

接口测试号申请

接入指南

接口配置测试

servlet代码

URL配置

P4 1.4 开发接入 20:25

sha1加密

验证签名、接入成功截图

WxServlet.java

WxService.java


P3 1.3 开发接入准备 19:08

基本配置

自己注册的公众号,很多权限都没有!

服务号 开发文档

微信公众开发平台:https://mp.weixin.qq.com/

  

接口测试号申请

接入指南

接口配置测试

servlet代码

未经修改的servlet代码:

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WxServlet
 */
@WebServlet("/wx")
public class WxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

URL配置

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/wx")
public class WxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 *  signature  微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
			timestamp  时间戳
			nonce	     随机数
			echostr	     随机字符串
		 */
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		System.out.println(signature);
		System.out.println(timestamp);
		System.out.println(nonce);
		System.out.println(echostr);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("post");
	}

}

P4 1.4 开发接入 20:25

sha1加密

SHA-1——百度百科

SHA-1(英语:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦数据处理标准(FIPS)。SHA-1可以生成一个被称为消息摘要的160(20字节)散列值,散列值通常的呈现形式为40个十六进制数。

验证签名、接入成功截图

WxServlet.java

package servlet;

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

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

import service.WxService;

@WebServlet("/wx")
public class WxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 
		 * timestamp 时间戳 
		 * nonce 随机数 
		 * echostr 随机字符串
		 */
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		System.out.println(signature);
		System.out.println(timestamp);
		System.out.println(nonce);
		System.out.println(echostr);
		// 校验签名
		if (WxService.check(timestamp, nonce, signature)) {
			System.out.println("接入成功!");
			PrintWriter out = response.getWriter();
			// 原样返回echostr参数
			out.print(echostr);
			out.flush();
			out.close();
		} else {
			System.out.println("接入失败!");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("post");
	}

}

WxService.java

package service;

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

public class WxService {
	private static final String TOKEN = "llzs";

	/**
	 * 验证签名
	 * @param timestamp
	 * @param nonce
	 * @param signature
	 * @return
	 */
	public static boolean check(String timestamp, String nonce, String signature) {
		// 1)将token、timestamp、nonce三个参数进行字典序排序
		String[] strs = new String[] { TOKEN, timestamp, nonce };
		Arrays.sort(strs);
		// 2)将三个参数字符串拼接成一个字符串进行sha1加密
		String str = strs[0] + strs[1] + strs[2];
		String mysig = sha1(str);
		// 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
		return mysig.equalsIgnoreCase(signature); // 进行字符串比对
	}

	/**
	 * 进行sha1加密
	 * 
	 * @param str
	 * @return
	 */
	private static String sha1(String src) {
		try {
			// 获取一个加密对象
			MessageDigest md = MessageDigest.getInstance("sha1"); // 获取加密对象
			// 加密
			byte[] digest = md.digest(src.getBytes());
			char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
			StringBuilder sb = new StringBuilder();
			// 处理加密结果
			for (byte b : digest) {
				sb.append(chars[(b >> 4) & 15]);
				sb.append(chars[b & 15]);
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

}

将啼饥者比,则得饱自乐;

将号寒者比,则得暖自乐;

将劳役者比,则优闲自乐;

将疾病者比,则康健自乐;

将祸患者比,则平安自乐;

将死亡者比,则生存自乐。

猜你喜欢

转载自blog.csdn.net/weixin_44949135/article/details/114162312