微信小程序Java后台获取手机号

小程序端:

复制代码
  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;
	}

在网上找了好久,东拼洗凑居然能用,希望对写小程序的朋友有帮助。

相关推荐
從南走到北1 天前
JAVA代驾小程序源码代驾跑腿APP源码
java·开发语言·微信·微信小程序·小程序
太过平凡的小蚂蚁2 天前
责任链模式:灵活可扩展的责任传递艺术(行为模式)
微信·责任链模式
孙严Pay2 天前
快捷支付和网关支付到底哪个更安全呢?
笔记·科技·计算机网络·其他·微信
陈思杰系统思考Jason3 天前
业务创新与系统思考
百度·微信·微信公众平台·新浪微博·微信开放平台
liugang_lawyer4 天前
防范新型金融诈骗
笔记·百度·微信·金融·微信公众平台·新浪微博
陈思杰系统思考Jason5 天前
系统思考:决策
百度·微信·微信公众平台·新浪微博·微信开放平台
优梦创客5 天前
3 天搞定微小游戏上架!独立开发者副业赚钱全攻略(附选题 / 技术 / 运营干货)
微信·教程·小游戏·游戏开发
Jing_Rainbow5 天前
【 Weapp-3 /Lesson20(2025-11-04)】路虎卫士小程序开发详解:从架构到细节的深度解析🚙📱
微信·微信小程序·程序员
Jing_Rainbow6 天前
【Weapp-2 /Lesson19(2025-11-04)】微信小程序“博客园”项目深度解析:从架构到细节的完整剖析📱
微信·微信小程序·程序员