微信小程序 Java 后台获取电话号码
最编程
2024-07-10 17:52:03
...
小程序端:
wx.request({
url: registerphone, //自己的地址
data: {
openid: openid,
encryptedData: encryptedData, //手机加密数据
iv: iv, // 加密iv
session_key: session_key,// 加密key
},
method: "post",
header: {
"content-type": "application/x-www-form-urlencoded",
},
success: (resp) => {
console.log(resp);// 返回手机号
},
});
springboot后台:
pom.xml maven 依赖包:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
http 后台代码:
@PostMapping("/registerphone")
public String registerphone(String openid, String encryptedData, String iv, String session_key)
{
String retS = "";
String appid = "wxc32bd5819d7a1cc5";
try{
JSONObject decryptObject = NativeUtils.decrypt(appid, encryptedData, session_key, iv);
//logger.info(decryptObject.toJSONString());
retS = decryptObject.getString("phoneNumber");
} catch (Exception ex){
}
return retS;
}
//---------------------------------------------------------------------------------
// 解密接口
/**
* 解密数据
* @return
* @throws Exception
*/
public static JSONObject decrypt(String appId, String encryptedData, String sessionKey, String iv){
try {
byte[] resultByte = decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(sessionKey),Base64.decodeBase64(iv));
if(null != resultByte && resultByte.length > 0){
String result = new String(WxPKCS7Encoder.decode(resultByte));
JSONObject jsonObject = JSONObject.parseObject(result);
String decryptAppId = jsonObject.getJSONObject("watermark").getString("appid");
if(!appId.equals(decryptAppId)){
return null;
}
return jsonObject;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
/**
* AES解密
*
* @param content
* 密文
* @return
* @throws InvalidAlgorithmParameterException
* @throws NoSuchProviderException
*/
public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
Key sKeySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
byte[] result = cipher.doFinal(content);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 生成iv
public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(new IvParameterSpec(iv));
return params;
}
在网上找了好久,东拼洗凑居然能用,希望对写小程序的朋友有帮助。
推荐阅读
-
如何在微信小程序中获取当前日期、时间和时间戳?
-
全面解析:如何在微信小程序中使用new Date获取时间
-
怎样获取仅在手机上可用的微信小程序数据?
-
如何在企业微信小程序中获取企业微信侧边栏的userid?
-
如何在微信小程序中获取和使用JavaScript里的数据
-
用Java实现流畅阅读体验的微信小程序第二篇:技术详解
-
小程序搜索栏新增 "搜索历史" | iOS 版微信更新至 6.6.0,客户端更新,公众号界面、后台改版
-
微信小程序下的nginx代理wss,实现与原服务协议ws、Java版本的兼容
-
基于 SSM vue 的 Java 毕业设计 电影预订系统小程序 微信小程序
-
微信小程序 Java 后台获取电话号码