微信小程序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;
	}

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

相关推荐
IT WorryFree1 小时前
如何设置微信群机器人
微信·github
非优秀程序员2 小时前
10分钟,用qclaw打造你的AI选股系统--官方每天送4百万token
人工智能·微信·产品经理
AI周红伟5 小时前
周红伟:OpenClaw 企业智能体:架构、治理与全球部署实战
人工智能·微信·架构·云计算·腾讯云·openclaw
gsls2008088 小时前
ubuntu安装openclaw接入智谱大模型和微信QQ通道配置
微信·qq·智谱·openclaw·龙虾
AI周红伟8 小时前
周红伟:OpenClaw 企业级智能体架构与全栈实战
人工智能·微信·架构·云计算·腾讯云·openclaw
陈思杰系统思考Jason8 小时前
局限思考的代价:团队内耗
百度·微信·微信公众平台·新浪微博·微信开放平台
Crazy CodeCrafter1 天前
房租年年涨,客流年年少,服装店还要开吗?
大数据·运维·微信·自动化·开源软件
陈思杰系统思考Jason1 天前
系统思考:组织学习与个人学习
百度·微信·微信公众平台·新浪微博·微信开放平台
羊群智妍1 天前
2026年AI搜索优化监测工具横评:免费到付费方案
百度·微信·微信公众平台·facebook·新浪微博
个微管理1 天前
微信小技巧:学会这四招,让你的微信“自动”替你打工
微信·智能手机·自动化·微信公众平台·微信开放平台