2021-11月新写
1>.小程序端请求后台接口,传递授权码code;
2>.后台接口根据code,appid,secret,以及grant_type获取用户openid,session_key信息并返回
3>.小程序调用微信api获取用户敏感信息加密串encryptedData以及偏移量iv将两者以及session_key返回到服务器中
4>.服务器根据敏感信息加密串encryptedData以及偏移量iv,session_key解析用户手机号
(前端调api然后获得加密后端信息传给后端解密)
@Override
public String wxFindPhone(String encryptedData, String sessionKey, String iv, String openid) {
JSONObject userInfo = WxUserInfoDecodeUtil.getUserInfo(encryptedData, sessionKey, iv);
logger.info("解密后的用户信息:", userInfo.toString());
if (null == userInfo) {
return WXPayConstants.FAIL;
}
String phone = userInfo.getString("phoneNumber");
// 先判断是否存储了,在通过openid存储phone-----------
Integer integer = userMapper.existUser(phone);
if (integer == null) {
ZycxUser zycxUser = new ZycxUser();
zycxUser.setOpenId(openid).setPhone(phone).setCreateTime(new Date());
userMapper.insertUser(zycxUser);
}
return userInfo.toString();
}
工具类
package com.zhongyun.zycx.utils;
import net.sf.json.JSONObject;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays;
/**
* 微信用户开放信息解密工具
*
* @author liangchao
* @version 1.0
* @date 2021/10/8 17:15
*/
public class WxUserOpenInfoDecodeUtil {
/**
* 微信用户开放信息解密
*
* @param encryptedData 敏感信息加密串
* @param sessionKey 授权后得到的key
* @param iv 偏移量
* @return
*/
public static JSONObject getUserInfo(String encryptedData,String sessionKey,String iv){
// 被加密的数据
byte[] dataByte = Base64.decode(encryptedData);
// 加密秘钥
byte[] keyByte = Base64.decode(sessionKey);
// 偏移量
byte[] ivByte = Base64.decode(iv);
try {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
return JSONObject.fromObject(result);
}
}catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
微信支付和退款,需要关注成为粉丝才可见哦!!
微信支付和退款,需要关注成为粉丝才可见哦!!
微信支付和退款,需要关注成为粉丝才可见哦!!