1.添加微信小程序验证登录
2.添加敏感词上传 3.保存工作描述时,验证敏感词
This commit is contained in:
@@ -11,10 +11,14 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
import java.util.Formatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -210,6 +214,60 @@ public class WechatUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过code获取微信用户的openid和session_key
|
||||
*
|
||||
* @param appid 小程序appid
|
||||
* @param secret 小程序secret
|
||||
* @param code 登录凭证code
|
||||
* @return 包含openid、session_key、unionid的JSON对象
|
||||
*/
|
||||
public JSONObject code2Session(String appid, String secret, String code) {
|
||||
try {
|
||||
String response = getAccessTokenData("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code");
|
||||
JSONObject result = JSONObject.parseObject(response);
|
||||
// 微信返回错误码处理
|
||||
if (result.containsKey("errcode") && result.getInteger("errcode") != 0) {
|
||||
throw new RuntimeException("微信授权失败:" + result.getString("errmsg"));
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("调用微信接口失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密微信用户手机号(用户通过 getPhoneNumber 组件授权后返回的加密数据)
|
||||
* @param encryptedData 微信返回的加密手机号数据
|
||||
* @param sessionKey 从 code2Session 接口获取的会话密钥
|
||||
* @param iv 微信返回的加密向量(与 encryptedData 配套)
|
||||
* @return 解密后的 JSON 对象(包含 phoneNumber、purePhoneNumber 等字段)
|
||||
* @throws RuntimeException 解密失败时抛出
|
||||
*/
|
||||
public JSONObject decryptPhoneNumber(String encryptedData, String sessionKey, String iv) {
|
||||
try {
|
||||
// 1. Base64 解码(encryptedData、sessionKey、iv 均为 Base64 编码)
|
||||
byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
|
||||
byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey);
|
||||
byte[] ivBytes = Base64.getDecoder().decode(iv);
|
||||
|
||||
// 2. 初始化 AES-128-CBC 解密器(微信固定加密算法)
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
|
||||
SecretKeySpec keySpec = new SecretKeySpec(sessionKeyBytes, "AES");
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
||||
|
||||
// 3. 执行解密并转换为字符串
|
||||
byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes);
|
||||
String decryptedStr = new String(decryptedBytes, StandardCharsets.UTF_8);
|
||||
|
||||
// 4. 解析为 JSON 并返回(包含手机号等信息)
|
||||
return JSONObject.parseObject(decryptedStr);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解密用户手机号失败:" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private String create_nonce_str() {
|
||||
return IdUtil.simpleUUID();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user