微信小程序消息推送代对接微信功能代码修改
This commit is contained in:
parent
317e38e594
commit
6341f71a25
@ -100,6 +100,8 @@ token:
|
||||
secret: DIweGcEWJTbvo48dnvOMR8GsDW
|
||||
# 令牌有效期(默认30分钟)
|
||||
expireTime: 30
|
||||
# 请求拦截白名单
|
||||
ant-matchers: /postDischarge/**
|
||||
|
||||
## MyBatis-Plus配置
|
||||
mybatis-plus:
|
||||
|
||||
@ -0,0 +1,55 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
/**
|
||||
* @Description 微信AES解密异常信息类
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:17:50
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class AesException extends Exception {
|
||||
|
||||
public final static int OK = 0;
|
||||
public final static int VALIDATE_SIGNATURE_ERROR = -40001;
|
||||
public final static int PARSE_XML_ERROR = -40002;
|
||||
public final static int COMPUTE_SIGNATURE_ERROR = -40003;
|
||||
public final static int ILLEGAL_AES_KEY = -40004;
|
||||
public final static int VALIDATE_APP_ID_ERROR = -40005;
|
||||
public final static int ENCRYPT_AES_ERROR = -40006;
|
||||
public final static int DECRYPT_AES_ERROR = -40007;
|
||||
public final static int ILLEGAL_BUFFER = -40008;
|
||||
|
||||
private final int code;
|
||||
|
||||
private static String getMessage(int code) {
|
||||
switch (code) {
|
||||
case VALIDATE_SIGNATURE_ERROR:
|
||||
return "签名验证错误";
|
||||
case PARSE_XML_ERROR:
|
||||
return "xml解析失败";
|
||||
case COMPUTE_SIGNATURE_ERROR:
|
||||
return "sha加密生成签名失败";
|
||||
case ILLEGAL_AES_KEY:
|
||||
return "SymmetricKey非法";
|
||||
case VALIDATE_APP_ID_ERROR:
|
||||
return "appid校验失败";
|
||||
case ENCRYPT_AES_ERROR:
|
||||
return "aes加密失败";
|
||||
case DECRYPT_AES_ERROR:
|
||||
return "aes解密失败";
|
||||
case ILLEGAL_BUFFER:
|
||||
return "解密后得到的buffer非法";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
AesException(int code) {
|
||||
super(getMessage(code));
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description 字节数组
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:17:56
|
||||
*/
|
||||
class ByteGroup {
|
||||
List<Byte> byteContainer = new ArrayList<>();
|
||||
|
||||
public byte[] toBytes() {
|
||||
byte[] bytes = new byte[byteContainer.size()];
|
||||
for (int i = 0; i < byteContainer.size(); i++) {
|
||||
bytes[i] = byteContainer.get(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public void addBytes(byte[] bytes) {
|
||||
for (byte b : bytes) {
|
||||
byteContainer.add(b);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return byteContainer.size();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @Description 提供基于PKCS7算法的加解密接口
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:18:21
|
||||
*/
|
||||
class PKCS7Encoder {
|
||||
static Charset CHARSET = StandardCharsets.UTF_8;
|
||||
static int BLOCK_SIZE = 32;
|
||||
|
||||
/**
|
||||
* 获得对明文进行补位填充的字节.
|
||||
*
|
||||
* @param count 需要进行填充补位操作的明文字节个数
|
||||
* @return 补齐用的字节数组
|
||||
*/
|
||||
static byte[] encode(int count) {
|
||||
// 计算需要填充的位数
|
||||
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
|
||||
if (amountToPad == 0) {
|
||||
amountToPad = BLOCK_SIZE;
|
||||
}
|
||||
// 获得补位所用的字符
|
||||
char padChr = chr(amountToPad);
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
for (int index = 0; index < amountToPad; index++) {
|
||||
tmp.append(padChr);
|
||||
}
|
||||
return tmp.toString().getBytes(CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除解密后明文的补位字符
|
||||
*
|
||||
* @param decrypted 解密后的明文
|
||||
* @return 删除补位字符后的明文
|
||||
*/
|
||||
static byte[] decode(byte[] decrypted) {
|
||||
int pad = decrypted[decrypted.length - 1];
|
||||
if (pad < 1 || pad > 32) {
|
||||
pad = 0;
|
||||
}
|
||||
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数字转化成ASCII码对应的字符,用于对明文进行补码
|
||||
*
|
||||
* @param a 需要转化的数字
|
||||
* @return 转化得到的字符
|
||||
*/
|
||||
static char chr(int a) {
|
||||
byte target = (byte) (a & 0xFF);
|
||||
return (char) target;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,94 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* SHA1 class
|
||||
* <p>
|
||||
*
|
||||
* @Description 计算公众平台的消息签名接口
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:19:50
|
||||
*/
|
||||
class SHA1 {
|
||||
|
||||
/**
|
||||
* 用SHA1算法生成安全签名
|
||||
*
|
||||
* @param token 票据
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @param encrypt 密文
|
||||
* @return 安全签名
|
||||
* @throws AesException 异常信息
|
||||
*/
|
||||
public static String getShaOne(String token, String timestamp, String nonce, String encrypt) throws AesException {
|
||||
return signatureByShaOne(token, timestamp, nonce, encrypt, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用SHA1算法生成安全签名,小程序回调设置验证URL时计算签名使用
|
||||
*
|
||||
* @param token 票据
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @return 安全签名
|
||||
* @throws AesException 异常信息
|
||||
*/
|
||||
public static String getShaTwo(String token, String timestamp, String nonce) throws AesException {
|
||||
return signatureByShaOne(token, timestamp, nonce, "", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用SHA-1计算签名公共方法
|
||||
*
|
||||
* @param token 令牌
|
||||
* @param timestamp 时间粗
|
||||
* @param nonce 随机字符串
|
||||
* @param encrypt 密文
|
||||
* @param encryptFlag 密文传入标识
|
||||
* @return String 明文
|
||||
* @throws AesException 异常信息
|
||||
*/
|
||||
private static String signatureByShaOne(String token, String timestamp, String nonce, String encrypt, boolean encryptFlag) throws AesException {
|
||||
try {
|
||||
String[] array;
|
||||
int size;
|
||||
if (BooleanUtils.isTrue(encryptFlag)) {
|
||||
array = new String[]{token, timestamp, nonce, encrypt};
|
||||
size = 4;
|
||||
} else {
|
||||
array = new String[]{token, timestamp, nonce};
|
||||
size = 3;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// 字符串排序
|
||||
Arrays.sort(array);
|
||||
for (int i = 0; i < size; i++) {
|
||||
sb.append(array[i]);
|
||||
}
|
||||
String str = sb.toString();
|
||||
// SHA1签名生成
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
md.update(str.getBytes());
|
||||
byte[] digest = md.digest();
|
||||
StringBuilder hexStr = new StringBuilder();
|
||||
String shaHex;
|
||||
for (byte b : digest) {
|
||||
shaHex = Integer.toHexString(b & 0xFF);
|
||||
if (shaHex.length() < 2) {
|
||||
hexStr.append(0);
|
||||
}
|
||||
hexStr.append(shaHex);
|
||||
}
|
||||
return hexStr.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new AesException(AesException.COMPUTE_SIGNATURE_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,234 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @Description 提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串).
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:18:26
|
||||
*/
|
||||
public class WXBizMsgCrypt {
|
||||
static Charset CHARSET = StandardCharsets.UTF_8;
|
||||
Base64 base64 = new Base64();
|
||||
byte[] aesKey;
|
||||
String token;
|
||||
String appId;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param token 公众平台上,开发者设置的token
|
||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
|
||||
* @param appId 公众平台appid
|
||||
* @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
|
||||
*/
|
||||
public WXBizMsgCrypt(String token, String encodingAesKey, String appId) throws AesException {
|
||||
int length = 43;
|
||||
if (encodingAesKey.length() != length) {
|
||||
throw new AesException(AesException.ILLEGAL_AES_KEY);
|
||||
}
|
||||
this.token = token;
|
||||
this.appId = appId;
|
||||
aesKey = Base64.decodeBase64(encodingAesKey + "=");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成4个字节的网络字节序
|
||||
*
|
||||
* @param sourceNumber 数值
|
||||
* @return 字节数组
|
||||
*/
|
||||
byte[] getNetworkBytesOrder(int sourceNumber) {
|
||||
byte[] orderBytes = new byte[4];
|
||||
orderBytes[3] = (byte) (sourceNumber & 0xFF);
|
||||
orderBytes[2] = (byte) (sourceNumber >> 8 & 0xFF);
|
||||
orderBytes[1] = (byte) (sourceNumber >> 16 & 0xFF);
|
||||
orderBytes[0] = (byte) (sourceNumber >> 24 & 0xFF);
|
||||
return orderBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 还原4个字节的网络字节序
|
||||
*
|
||||
* @param orderBytes 字节数组
|
||||
* @return 数值
|
||||
*/
|
||||
int recoverNetworkBytesOrder(byte[] orderBytes) {
|
||||
int sourceNumber = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
sourceNumber <<= 8;
|
||||
sourceNumber |= orderBytes[i] & 0xff;
|
||||
}
|
||||
return sourceNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成16位字符串
|
||||
*
|
||||
* @return 随机字符串
|
||||
*/
|
||||
String getRandomStr() {
|
||||
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
Random random = new Random();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int number = random.nextInt(base.length());
|
||||
sb.append(base.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对明文进行加密.
|
||||
*
|
||||
* @param text 需要加密的明文
|
||||
* @return 加密后base64编码的字符串
|
||||
* @throws AesException aes加密失败
|
||||
*/
|
||||
String encrypt(String randomStr, String text) throws AesException {
|
||||
ByteGroup byteCollector = new ByteGroup();
|
||||
byte[] randomStrBytes = randomStr.getBytes(CHARSET);
|
||||
byte[] textBytes = text.getBytes(CHARSET);
|
||||
byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length);
|
||||
byte[] appidBytes = appId.getBytes(CHARSET);
|
||||
// randomStr + networkBytesOrder + text + appid
|
||||
byteCollector.addBytes(randomStrBytes);
|
||||
byteCollector.addBytes(networkBytesOrder);
|
||||
byteCollector.addBytes(textBytes);
|
||||
byteCollector.addBytes(appidBytes);
|
||||
// ... + pad: 使用自定义的填充方式对明文进行补位填充
|
||||
byte[] padBytes = PKCS7Encoder.encode(byteCollector.size());
|
||||
byteCollector.addBytes(padBytes);
|
||||
// 获得最终的字节流, 未加密
|
||||
byte[] unencrypted = byteCollector.toBytes();
|
||||
try {
|
||||
// 设置加密模式为AES的CBC模式
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
|
||||
IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
|
||||
// 加密
|
||||
byte[] encrypted = cipher.doFinal(unencrypted);
|
||||
// 使用BASE64对加密后的字符串进行编码
|
||||
return base64.encodeToString(encrypted);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new AesException(AesException.ENCRYPT_AES_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对密文进行解密.
|
||||
*
|
||||
* @param text 需要解密的密文
|
||||
* @return 解密得到的明文
|
||||
* @throws AesException aes解密失败
|
||||
*/
|
||||
String decrypt(String text) throws AesException {
|
||||
byte[] original;
|
||||
try {
|
||||
// 设置解密模式为AES的CBC模式
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
|
||||
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
|
||||
// 使用BASE64对密文进行解码
|
||||
byte[] encrypted = Base64.decodeBase64(text);
|
||||
// 解密
|
||||
original = cipher.doFinal(encrypted);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new AesException(AesException.DECRYPT_AES_ERROR);
|
||||
}
|
||||
String xmlContent, fromAppid;
|
||||
try {
|
||||
// 去除补位字符
|
||||
byte[] bytes = PKCS7Encoder.decode(original);
|
||||
// 分离16位随机字符串,网络字节序和AppId
|
||||
byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
|
||||
int xmlLength = recoverNetworkBytesOrder(networkOrder);
|
||||
xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
|
||||
fromAppid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length),
|
||||
CHARSET);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new AesException(AesException.ILLEGAL_BUFFER);
|
||||
}
|
||||
// appid不相同的情况
|
||||
if (!fromAppid.equals(appId)) {
|
||||
throw new AesException(AesException.VALIDATE_APP_ID_ERROR);
|
||||
}
|
||||
return xmlContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param replyMsg 公众平台待回复用户的消息,xml格式的字符串
|
||||
* @param timeStamp 时间戳,可以自己生成,也可以用URL参数的timestamp
|
||||
* @param nonce 随机串,可以自己生成,也可以用URL参数的nonce
|
||||
* @return 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
|
||||
* @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
|
||||
* @Description 将公众平台回复用户的消息加密打包.
|
||||
*/
|
||||
public String encryptMsg(String replyMsg, String timeStamp, String nonce) throws AesException {
|
||||
// 加密
|
||||
String encrypt = encrypt(getRandomStr(), replyMsg);
|
||||
// 生成安全签名
|
||||
if ("".equals(timeStamp)) {
|
||||
timeStamp = Long.toString(System.currentTimeMillis());
|
||||
}
|
||||
String signature = SHA1.getShaOne(token, timeStamp, nonce, encrypt);
|
||||
// 生成发送的xml
|
||||
return XMLParse.generate(encrypt, signature, timeStamp, nonce);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msgSignature 签名串,对应URL参数的msg_signature
|
||||
* @param timeStamp 时间戳,对应URL参数的timestamp
|
||||
* @param nonce 随机串,对应URL参数的nonce
|
||||
* @param postData 密文,对应POST请求的数据
|
||||
* @return 解密后的原文
|
||||
* @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
|
||||
* @Description 检验消息的真实性,并且获取解密后的明文.
|
||||
*/
|
||||
public String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData)
|
||||
throws AesException {
|
||||
// 密钥,公众账号的app secret
|
||||
// 提取密文
|
||||
Object[] encrypt = XMLParse.extract(postData);
|
||||
// 验证安全签名
|
||||
String signature = SHA1.getShaTwo(token, timeStamp, nonce);
|
||||
// 和URL中的签名比较是否相等
|
||||
if (!signature.equals(msgSignature)) {
|
||||
throw new AesException(AesException.VALIDATE_SIGNATURE_ERROR);
|
||||
}
|
||||
// 解密
|
||||
return decrypt(encrypt[1].toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证URL
|
||||
*
|
||||
* @param msgSignature 签名串,对应URL参数的msg_signature
|
||||
* @param timeStamp 时间戳,对应URL参数的timestamp
|
||||
* @param nonce 随机串,对应URL参数的nonce
|
||||
* @param echoStr 随机串,对应URL参数的echostr
|
||||
* @return 原始的随机串echostr
|
||||
* @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
|
||||
*/
|
||||
public String verifyUrl(String msgSignature, String timeStamp, String nonce, String echoStr) throws AesException {
|
||||
String signature = SHA1.getShaTwo(token, timeStamp, nonce);
|
||||
if (!signature.equals(msgSignature)) {
|
||||
throw new AesException(AesException.VALIDATE_SIGNATURE_ERROR);
|
||||
}
|
||||
return echoStr;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
package com.xinelu.common.utils.aes;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* @Description 提供提取消息格式中的密文及生成回复消息格式的接口.
|
||||
* @Author WeChat
|
||||
* @Date 2024-03-21 10:18:57
|
||||
*/
|
||||
class XMLParse {
|
||||
|
||||
/**
|
||||
* 提取出xml数据包中的加密消息
|
||||
*
|
||||
* @param xmltext 待提取的xml字符串
|
||||
* @return 提取出的加密消息字符串
|
||||
* @throws AesException 异常信息
|
||||
*/
|
||||
public static Object[] extract(String xmltext) throws AesException {
|
||||
Object[] result = new Object[3];
|
||||
try {
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||
dbf.setXIncludeAware(false);
|
||||
dbf.setExpandEntityReferences(false);
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
StringReader sr = new StringReader(xmltext);
|
||||
InputSource is = new InputSource(sr);
|
||||
Document document = db.parse(is);
|
||||
Element root = document.getDocumentElement();
|
||||
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
|
||||
NodeList nodelist2 = root.getElementsByTagName("ToUserName");
|
||||
result[0] = 0;
|
||||
result[1] = nodelist1.item(0).getTextContent();
|
||||
result[2] = nodelist2.item(0).getTextContent();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new AesException(AesException.PARSE_XML_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成xml消息
|
||||
*
|
||||
* @param encrypt 加密后的消息密文
|
||||
* @param signature 安全签名
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @return 生成的xml字符串
|
||||
*/
|
||||
public static String generate(String encrypt, String signature, String timestamp, String nonce) {
|
||||
String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n"
|
||||
+ "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n"
|
||||
+ "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n" + "</xml>";
|
||||
return String.format(format, encrypt, signature, timestamp, nonce);
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ import com.xinelu.framework.config.properties.PermitAllUrlProperties;
|
||||
import com.xinelu.framework.security.filter.JwtAuthenticationTokenFilter;
|
||||
import com.xinelu.framework.security.handle.AuthenticationEntryPointImpl;
|
||||
import com.xinelu.framework.security.handle.LogoutSuccessHandlerImpl;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
@ -64,6 +65,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Resource
|
||||
private PermitAllUrlProperties permitAllUrl;
|
||||
|
||||
/**
|
||||
* 请求拦截白名单
|
||||
*/
|
||||
@Value("${token.ant-matchers}")
|
||||
private String antMatchers;
|
||||
|
||||
/**
|
||||
* 解决 无法直接注入 AuthenticationManager
|
||||
*
|
||||
@ -111,6 +118,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
// 静态资源,可匿名访问
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
||||
.antMatchers(antMatchers.split(",")).permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
||||
@ -0,0 +1,74 @@
|
||||
package com.xinelu.mobile.controller.wechatappletcallback;
|
||||
|
||||
import com.xinelu.common.config.AppletChatConfig;
|
||||
import com.xinelu.common.utils.aes.AesException;
|
||||
import com.xinelu.common.utils.aes.WXBizMsgCrypt;
|
||||
import com.xinelu.mobile.dto.wechatappletcallback.MessageSignDTO;
|
||||
import com.xinelu.mobile.service.wechatappletcallback.WeChatAppletCallBackService;
|
||||
import com.xinelu.mobile.utils.XmlUtil;
|
||||
import com.xinelu.mobile.vo.wechatappletcallback.WeChatMessagePushVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Description 院后微信小程序事件回调控制器
|
||||
* @Author 纪寒
|
||||
* @Date 2024-03-21 10:20:32
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/postDischarge/weChatAppletCallBack")
|
||||
public class WeChatAppletCallBackController {
|
||||
|
||||
@Resource
|
||||
private AppletChatConfig appletChatConfig;
|
||||
@Resource
|
||||
private WeChatAppletCallBackService weChatAppletCallBackService;
|
||||
|
||||
/**
|
||||
* 微信小程序回调验证方法
|
||||
*
|
||||
* @param messageSignDTO 微信输入参数
|
||||
* @return 解密后的字符串
|
||||
* @throws AesException 异常信息
|
||||
*/
|
||||
@GetMapping
|
||||
public String getWeChatAppletCallBack(MessageSignDTO messageSignDTO) throws AesException {
|
||||
WXBizMsgCrypt wxCpt = new WXBizMsgCrypt(appletChatConfig.getToken(), appletChatConfig.getEncodingAesKey(), appletChatConfig.getAppletId());
|
||||
String verifyMessage = wxCpt.verifyUrl(messageSignDTO.getSignature(), messageSignDTO.getTimestamp(), messageSignDTO.getNonce(), messageSignDTO.getEchostr());
|
||||
log.info("院后微信小程序回调设置验证URL成功,验证信息:verifyMessage = [{}]", verifyMessage);
|
||||
return verifyMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序消息推送事件回调POST处理
|
||||
*
|
||||
* @param signature 签名
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机字符串
|
||||
* @param postData 消息体,xml格式
|
||||
*/
|
||||
@PostMapping
|
||||
public void handleWeChatAppletCallBack(@RequestParam("signature") String signature,
|
||||
@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestBody String postData) throws AesException {
|
||||
WXBizMsgCrypt wxCpt = new WXBizMsgCrypt(appletChatConfig.getToken(), appletChatConfig.getEncodingAesKey(), appletChatConfig.getAppletId());
|
||||
String decryptMsg = wxCpt.decryptMsg(signature, timestamp, nonce, postData);
|
||||
WeChatMessagePushVO weChatMessagePushVO = (WeChatMessagePushVO) XmlUtil.fromXml(decryptMsg, WeChatMessagePushVO.class);
|
||||
if (Objects.isNull(weChatMessagePushVO)) {
|
||||
log.error("院后微信小程序xml数据转换失败,请求信息为: [{}]", decryptMsg);
|
||||
return;
|
||||
}
|
||||
if (StringUtils.isBlank(weChatMessagePushVO.getEvent()) || StringUtils.isBlank(weChatMessagePushVO.getMsgType())) {
|
||||
return;
|
||||
}
|
||||
log.info("院后微信小程序消息推送回调执行,消息数据为: [{}]", weChatMessagePushVO);
|
||||
weChatAppletCallBackService.handleWeChatAppletCallBack(weChatMessagePushVO);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.xinelu.mobile.dto.wechatappletcallback;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Description 消息验证实体类
|
||||
* @Author 纪寒
|
||||
* @Date 2024-03-21 10:09:01
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
public class MessageSignDTO implements Serializable {
|
||||
private static final long serialVersionUID = 357274374767303359L;
|
||||
/**
|
||||
* 微信小程序签名
|
||||
*/
|
||||
private String signature;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private String timestamp;
|
||||
|
||||
/**
|
||||
* 随机字符串
|
||||
*/
|
||||
private String nonce;
|
||||
|
||||
/**
|
||||
* 加密的字符串
|
||||
*/
|
||||
private String echostr;
|
||||
|
||||
/**
|
||||
* 回调数据包,xml格式
|
||||
*/
|
||||
private String postData;
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package com.xinelu.mobile.utils;
|
||||
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
/**
|
||||
* @Description Xml格式数据工具类
|
||||
* @Author 纪寒
|
||||
* @Date 2024-03-21 10:22:34
|
||||
* @Version 1.0
|
||||
*/
|
||||
public class XmlUtil {
|
||||
/**
|
||||
* Xml数据转换
|
||||
*
|
||||
* @param xml 数据格式
|
||||
* @param objClass 要转换的类
|
||||
* @return 返回结果
|
||||
*/
|
||||
public static Object fromXml(String xml, Class objClass) {
|
||||
Serializer serializer = new Persister();
|
||||
try {
|
||||
return serializer.read(objClass, xml);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@ import java.util.List;
|
||||
@Root(name = "SubscribeMsgChangeEvent", strict = false)
|
||||
@Data
|
||||
public class SubscribeMsgChangeEventVO implements Serializable {
|
||||
private static final long serialVersionUID = -6682105837610124794L;
|
||||
private static final long serialVersionUID = 1500711025576966938L;
|
||||
/**
|
||||
* 信息集合
|
||||
*/
|
||||
|
||||
@ -15,7 +15,7 @@ import java.io.Serializable;
|
||||
@Root(name = "List", strict = false)
|
||||
@Data
|
||||
public class SubscribeMsgPopupEventListVO implements Serializable {
|
||||
private static final long serialVersionUID = 548605591615555467L;
|
||||
private static final long serialVersionUID = -4591923245411784629L;
|
||||
/**
|
||||
* 模板id
|
||||
*/
|
||||
|
||||
@ -16,7 +16,7 @@ import java.util.List;
|
||||
@Root(name = "SubscribeMsgPopupEvent", strict = false)
|
||||
@Data
|
||||
public class SubscribeMsgPopupEventVO implements Serializable {
|
||||
private static final long serialVersionUID = -6682105837610124794L;
|
||||
private static final long serialVersionUID = -2068512537328225075L;
|
||||
/**
|
||||
* 信息集合
|
||||
*/
|
||||
|
||||
@ -15,7 +15,7 @@ import java.io.Serializable;
|
||||
@Root(name = "List", strict = false)
|
||||
@Data
|
||||
public class SubscribeMsgSentEventListVO implements Serializable {
|
||||
private static final long serialVersionUID = 3771741965224817805L;
|
||||
private static final long serialVersionUID = -2104837918391487594L;
|
||||
/**
|
||||
* 模板id
|
||||
*/
|
||||
|
||||
@ -16,7 +16,7 @@ import java.util.List;
|
||||
@Root(name = "SubscribeMsgSentEvent", strict = false)
|
||||
@Data
|
||||
public class SubscribeMsgSentEventVO implements Serializable {
|
||||
private static final long serialVersionUID = -6682105837610124794L;
|
||||
private static final long serialVersionUID = -8280763002307815282L;
|
||||
/**
|
||||
* 信息集合
|
||||
*/
|
||||
|
||||
@ -15,7 +15,7 @@ import java.io.Serializable;
|
||||
@Root(name = "xml", strict = false)
|
||||
@Data
|
||||
public class WeChatMessagePushVO implements Serializable {
|
||||
private static final long serialVersionUID = 6233209958847696141L;
|
||||
private static final long serialVersionUID = 7102691058196437738L;
|
||||
/**
|
||||
* 小程序账号
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user