前言:起因是打开登录页面,第一次点击登录 请求后端接口会报解密异常,第二次解密正常,再点还是解密正常
解决方法: 把wxcode
在页面打开时,加载一次,然后由调用获取用户信息接口,把这个code
,encryptedData
,iv
传递给后端,后端请求jscode2session
获得sessionKey,把sessionKey
,encryptedData
,iv
,传入微信解密工具类,返回解密数据,剩下的就正常存储用户信息,返回信息就行了
前端相关代码展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| export default { data() { return { wxCode: '', encryptedData: '', iv: '' } }, onLoad() { let self = this; wx.login({ success(res) { if (res.code) { self.wxCode = res.code } } }) }, methods: { wxLogin() { const self = this; wx.getUserProfile({ desc: '用于完善会员资料', success: (res) => { let param = { wxCode: this.wxCode, encryptedData: res.encryptedData, iv: res.iv, }; let _callback = (res) => { console.log(res) }; self.ajax('post', '/weChatLogin', param, _callback, self); } }); } } }
|
后端相关代码展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public static String decryptWXAppletInfo(String sessionKey, String encryptedData, String iv) { String result = null; byte[] dataByte = java.util.Base64.getDecoder().decode(encryptedData); byte[] keyByte = java.util.Base64.getDecoder().decode(sessionKey); byte[] ivByte = Base64.getDecoder().decode(iv); try { 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; } AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivByte); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] doFinal = cipher.doFinal(dataByte); result = new String(doFinal); return result; } catch (Exception e) { log.error("解密,获取微信信息错误", e); } return null; } ```
|