Java獲取微信公眾號新增用戶事件


一、新建項目工程

  1. 新建一個spring項目

  2. 填寫 Group 和 Artifact 信息

  3. 這步可以直接跳過,后面再按需導入

  4. 選擇工程地址
    在這里插入圖片描述

二、配置

pom.xml

<dependencies>
    <!-- spring相關包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- http請求 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.6</version>
    </dependency>
    <!-- 工具包 -->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.5.8</version>
    </dependency>
    <!-- xml解析工具包 -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
</dependencies>

application.yml

spring:
  application:
    name: wechat

server:
  port: 8900

appid: # 微信開發者appid
secret: # 微信開發者appsecret
token: # 服務器校驗token

三、相關類

在這里插入圖片描述

AccessToken / AccessTokenInfo

/**
 * AccessToken 實體類
 * @author unidentifiable
 * @date 2021-02-28
 */
public class AccessToken {
    /**
     * 獲取到的憑證
     */
    private String tokenName;
    /**
     * 憑證有效時間  單位:秒
     */
    private int expireSecond;

	/**
	 * get/set ...
	 */
}

/**
 * AccessToken 實體類
 * @author unidentifiable
 * @date 2021-02-28
 */
public class AccessTokenInfo {
    /**
     * accessToken:像微信端發起請求需攜帶該accessToken
     */
	public static AccessToken accessToken = null;
}

MessageUtil

/**
 * 信息工具類
 * @author unidentifiable
 * @date 2021-02-28
 */
public class MessageUtil {
    /**
     * 解析微信發來的請求(XML)
     *
     * @param request 請求
     * @return map
     * @throws Exception 異常
     */
    public static Map<String, String> parseXml(HttpServletRequest request) {
        // 將解析結果存儲在HashMap中
        Map<String, String> map = new HashMap<>(16);
        // 從request中取得輸入流
        try (InputStream inputStream = request.getInputStream()) {
            System.out.println("獲取輸入流");
            // 讀取輸入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子節點
            List<Element> elementList = root.elements();

            // 遍歷所有子節點
            for (Element e : elementList) {
                System.out.println(e.getName() + " | " + e.getText());
                map.put(e.getName(), e.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 獲取用戶詳情
     * @param openId 微信公眾號用戶ID
     * @return 用戶詳情
     */
    public static String getUserInfo(String openId) {
        // 拼接 url,發起 http.get 請求
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + AccessTokenInfo.accessToken.getTokenName() + "&openid=" + openId + "&lang=zh_CN";
        return HttpUtil.get(url);
    }
}

AccessTokenConfig

/**
 * AccessToken 配置類
 *
 * @author unidentifiable
 * @date 2021-02-28
 */
public class AccessTokenConfig {

    static {
        Properties prop = new Properties();
        String appid = null;
        String secret = null;
        try {
            // 讀取 application.yml 獲取微信開發者 appid 和 appsecret
            InputStream in = MessageUtil.class.getClassLoader().getResourceAsStream("application.yml");
            prop.load(in);
            appid = prop.getProperty("appid");
            secret = prop.getProperty("secret");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 接口地址為https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET,其中grant_type固定寫為client_credential即可。
        String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, secret);
        // 此請求為https的get請求,返回的數據格式為{"access_token":"ACCESS_TOKEN","expires_in":7200}
        String result = HttpUtil.get(url);

        System.out.println("獲取到的access_token=" + result);

        //使用FastJson將Json字符串解析成Json對象
        JSONObject json = new JSONObject(result);
        AccessToken token = new AccessToken();
        token.setTokenName(json.get("access_token", String.class));
        token.setExpireSecond(json.get("expires_in", Integer.class));

        // 開啟一個線程,循環更新 AccessToken,該值有效期 2小時,需手動刷新
        new Thread(() -> {
            while (true) {
                try {
                    // 獲取accessToken
                    AccessTokenInfo.accessToken = token;
                    // 獲取成功
                    if (AccessTokenInfo.accessToken != null) {
                        // 獲取到access_token 休眠7000秒,大約2個小時左右
                        Thread.sleep(7000 * 1000);
                    } else {
                        // 獲取的access_token為空 休眠3秒
                        Thread.sleep(3000);
                    }
                } catch (Exception e) {
                    System.out.println("發生異常:" + e.getMessage());
                    e.printStackTrace();
                    try {
                        // 發生異常休眠1秒
                        Thread.sleep(1000);
                    } catch (Exception e1) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

SubscribeService

package com.unidentifiable.wechat.service;

import com.unidentifiable.wechat.util.MessageUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;

/**
 * Service
 * @author unidentifiable
 * @date 2021-02-28
 */
@Service("subscribeService")
public class SubscribeService {
    @Value("${subscriptionKey}")
    public String subscriptionKey;
    @Value("${uriBase}")
    public String uriBase;
    @Value("${token}")
    public String token;

    /**
     * 公眾號服務器校驗
     *
     * @param req 請求
     * @return 校驗結果
     */
    public String verification(HttpServletRequest req) {
        // 接收微信服務器發送請求時傳遞過來的參數
        String signature = req.getParameter("signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = req.getParameter("echostr");

        // 將token、timestamp、nonce三個參數進行字典序排序,並拼接為一個字符串
        String sortStr = sort(token, timestamp, nonce);
        // 字符串進行shal加密
        String mySignature = shal(sortStr);
        // 校驗微信服務器傳遞過來的簽名 和  加密后的字符串是否一致, 若一致則簽名通過
        if (!"".equals(signature) && !"".equals(mySignature) && signature.equals(mySignature)) {
            System.out.println("-----簽名校驗通過-----");
            return echostr;
        } else {
            System.out.println("-----校驗簽名失敗-----");
            return "";
        }
    }

    /**
     * 參數排序
     *
     * @param token     自定義token
     * @param timestamp timestamp
     * @param nonce     nonce
     * @return 排序后拼接字符串
     */
    public String sort(String token, String timestamp, String nonce) {
        String[] strArray = {token, timestamp, nonce};
        Arrays.sort(strArray);
        StringBuilder sb = new StringBuilder();
        for (String str : strArray) {
            sb.append(str);
        }
        return sb.toString();
    }

    /**
     * 字符串進行shal加密
     *
     * @param str 字符串
     * @return 密文
     */
    public String shal(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte messageDigest[] = digest.digest();

            StringBuilder hexString = new StringBuilder();
            // 字節數組轉換為 十六進制 數
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 關注請求
     *
     * @param req 請求
     * @return 關注事件結果
     */
    public String subscribeEvent(HttpServletRequest req) {
        String result = null;
        Map<String, String> map = null;

        try {
            /*
                解析請求,得到相關信息
                FromUserName| openid 用戶ID
                CreateTime|1614499703 時間
                MsgType|event 消息類型
                Event|subscribe 事件類型
                EventKey|
             */
            map = MessageUtil.parseXml(req);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 新開一個線程防止當前現在延時導致微信重復發起請求
        final Map<String, String> m = map;
        final String[] str = new String[1];
        new Thread(() -> {
            // 獲取判斷是否為關注事件
            if (null != m && "event".equals(m.get("MsgType")) && "subscribe".equals(m.get("Event"))) {
                str[0] = MessageUtil.getUserInfo(m.get("FromUserName"));
                System.out.println(str[0]);
                
                // 這里可以添加郵件通知等功能,也可以判斷別的事件,做出對應操作
            }
        }).start();

        return result;
    }

}

SubscribeController

package com.unidentifiable.wechat.controller;

import com.unidentifiable.wechat.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller
 * @author unidentifiable
 * @date 2021-02-28
 */
public class SubscribeController {
    @Autowired
    private SubscribeService subscribeService;

    /**
     * 服務器校驗
     * @param req 請求
     * @return 校驗結果
     */
    @GetMapping("/")
    public String verification(HttpServletRequest req) {
        return subscribeService.verification(req);
    }

    /**
     * 消息事件
     * @param req 請求
     * @return 結果
     */
    @PostMapping("/")
    public boolean subscribeEvent(HttpServletRequest req) {
        String s = subscribeService.subscribeEvent(req);
        return null != s;
    }
}

四、內網穿透(有域名的這一步可以省略)

這里使用的是 natapp 這一款工具,下載好對應版本工具

https://natapp.cn/

新建一個免費的隧道,配置好需要映射的地址跟端口,然后復制 authtoken

修改客戶端配置文件中的 authtoken(我這里是win版本,linux版本沒有該文件,建議新增一份,不然后台啟動可能會連接超時)

config.ini

#將本文件放置於natapp同級目錄 程序將讀取 [default] 段
#在命令行參數模式如 natapp -authtoken=xxx 等相同參數將會覆蓋掉此配置
#命令行參數 -config= 可以指定任意config.ini文件
[default]
authtoken=                      #對應一條隧道的authtoken
clienttoken=                    #對應客戶端的clienttoken,將會忽略authtoken,若無請留空,
log=none                        #log 日志文件,可指定本地文件, none=不做記錄,stdout=直接屏幕輸出 ,默認為none
loglevel=DEBUG                  #日志等級 DEBUG, INFO, WARNING, ERROR 默認為 DEBUG
http_proxy=                     #代理設置 如 http://10.123.10.10:3128 非代理上網用戶請務必留空

配置完成直接雙擊運行即可,linux版后台啟動可用 (./natapp &) 命令運行,注意是有括號的。出現如下界面即為成功。

五、微信公眾平台接口測試賬號

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

拿到 appID 跟 appsecret 填入 appliation.yml 配置文件中,啟動項目,啟動 natapp ,填入域名以及自定義的token 進行接口配置校驗。

綁定完成服務器信息之后,就可以掃描下面的二維碼進行測試了。

教程到這里就結束了,大家有想實現什么自定義功能的話可以查看微信官方文檔,看里面有提供什么功能,然后自己再修改下對應的判斷即可。

項目地址: https://gitee.com/unidentifiable/wechatpublicaccount


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM