微信小程序JAVA端手机号授权

 

是这样的,我没有做过微信小程序,我有一个接口,授权手机号码,今天看了很多笔记

大致流程是这样:

1.前端调接口,得到Code等返回信息

2.后台通过Code得到加密信息,sessionKey,openID等

3.后台解密得到信息,返回

 

但是我的接口文档,写着,就传一个手机号给我?不是还需要其他参数吗?

而且这些步骤都对应一个接口,按道理来说,在下图的加密信息解密Controller,不是已经将用户信息全部返回了吗?

那我这个授权手机号接口,到底是干什么?完全不懂,有大佬解释一下吗?跪谢

 

你上面的流程是对的,前端请求后端接口时传递

sessionKey、phoneEncryptedData、phoneEncryptedIv、userEncryptedData和userEncryptedIv

后端拿到数据后进行解密即可:

sessionKey、userEncryptedData和userEncryptedIv可以对应解析出用户信息(openid、unoinid、nickName和avatarUrl);sessionKey、phoneEncryptedData、phoneEncryptedIv可以解密出用户手机号码purePhoneNumber

/**
     * 小程序 数据解密
     *
     * @param encryptData 加密数据
     * @param iv          对称解密算法初始向量
     * @param sessionKey  对称解密秘钥
     * @return 解密数据
     */
    public static String wxDecrypt(String encryptData, String iv, String sessionKey) throws Exception {
        AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("AES");
        algorithmParameters.init(new IvParameterSpec(Base64.decodeBase64(iv)));
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), algorithmParameters);
        byte[] decode = PKCS7Decode(cipher.doFinal(Base64.decodeBase64(encryptData)));
        return new String(decode, StandardCharsets.UTF_8);
    }

得到用户手机号和用户openid/unionId后就可以对应登录或者注册了。