Mini program development configuration subscription message push [java version]

When developing message push, the mini program needs to apply for a template first. When applying for the first time, you need to configure some parameters.
Insert image description here
1. Configure the server URL, Token, secret key, encryption method, and data format.
Insert image description here
Token is used for verification, and the secret key is randomly generated. That's fine. I won't mention the encryption method. The data format should be according to your own preferences. I chose json.
2. Create an interface and write logic (it is estimated that friends who come to check the information have already fallen into the trap. Yes, after the above configuration is configured, you need to follow the provided URL address. WeChat will send a request and then interact with you to prove that you provided The URL is correct and available, otherwise you will not be allowed to submit the configuration!)
2.1. Tools

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

public class CheckUtils {
    
    

	private static String token = "mote"; // 定义Token 务必与服务器保持一致

	/**
	 * 验证签名
	 *
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @return
	 */
	public static boolean checkSignature(String signature, String timestamp,
			String nonce) {
    
    

		// 将token、timestamp、nonce三个参数进行字典排序
		String[] arr = new String[] {
    
     token, timestamp, nonce };
		Arrays.sort(arr);

		// 将三个参数字符串拼接成一个字符串
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
    
    
			content.append(arr[i]);
		}
		try {
    
    
			//获取加密工具
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			// 对拼接好的字符串进行sha1加密
			byte[] digest = md.digest(content.toString().getBytes());
			String tmpStr = byteToStr(digest);
			//获得加密后的字符串与signature对比
			return tmpStr != null ? tmpStr.equals(signature.toUpperCase()): false;
		} catch (NoSuchAlgorithmException e) {
    
    
			e.printStackTrace();
		}
		return false;
	}

	private static String byteToStr(byte[] byteArray) {
    
    
		String strDigest = "";
		for (int i = 0; i < byteArray.length; i++) {
    
    
			strDigest += byteToHexStr(byteArray[i]);
		}
		return strDigest;
	}

	private static String byteToHexStr(byte mByte) {
    
    
		char[] Digit = {
    
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
		tempArr[1] = Digit[mByte & 0X0F];
		String s = new String(tempArr);
		return s;
	}

}

2.2. Test interface

@RestController
@RequestMapping("/wxPush")
public class Controller {
    
    
    @GetMapping
    @ResponseBody
    public void openPushMsg(HttpServletRequest request,
                            HttpServletResponse response) {
    
    

        // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        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 (CheckUtils.checkSignature(signature, timestamp, nonce)) {
    
    
                out.print(echostr);
                out.flush();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            out.close();
        }

    }

}

3. Just package the project to the server. It doesn't matter whether you use nginx or tomcat, or directly java -jar. As long as the request goes through, 4. Configure the
server URL that was not filled in in the first step
Insert image description here
and click submit directly.
Insert image description here

At the end of the article, thank you Brother Chicken! ! !

Guess you like

Origin blog.csdn.net/qq_45699784/article/details/121790518