做微信小程序时,记录实现微信小程序登录
这里已经获取用户openId和sessionKey
1、通过按钮触发手机号授权
必须要通过按钮触发授权,open-type='getPhoneNumber'不能少
<button open-type='getPhoneNumber' @getphonenumber='onGetPhoneNumber'>手机号授权登录</button>
需要通过后台进行解析
onGetPhoneNumber(e) { if (e.detail.errMsg == "getPhoneNumber:fail user deny") { // 用户拒绝授权 // 拒绝授权后弹出一些提示 uni.showToast({ icon: 'error', duration: 2000, title: '登录失败' }) } else { // 允许授权 console.log('-加密的用户信息-', e.detail) // e.detail.encryptedData //加密的用户信息 // e.detail.iv //加密算法的初始向量 时要用到 // 调用接口获取手机号并修改小程序登录状态及相关信息 this.$myRequest({ url: '后台接口url(去后台解析加密的用户信息)', data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: that.sessionKey } }).then(rest => { console.log('返回的手机号', rest.message); }) } }
后台解析springboot
AESUtils工具
public class AESUtils { public static boolean initialized = false; /** * AES解密 * * @param content 密文 * @return * @throws InvalidAlgorithmParameterException * @throws NoSuchProviderException */ public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException { initialize(); 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; } public static void initialize() { if (initialized) { return; } Security.addProvider(new BouncyCastleProvider()); initialized = true; } //生成iv public static AlgorithmParameters generateIV(byte[] iv) throws Exception { AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(iv)); return params; } }
@GetMapping(value = "/getPhoneNumber") public Result getPhoneNumber(@RequestParam(required = true, value = "encryptedData") String encryptedData, @RequestParam(required = true, value = "iv") String iv, @RequestParam(required = true, value = "sessionKey") String sessionKey ) throws UnsupportedEncodingException, InvalidAlgorithmParameterException, JSONException { Result result = new Result(); //AESUtils微信获取手机号解密工具类 AESUtils aes = new AESUtils(); //调用AESUtils工具类decrypt方法解密获取json串 byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv)); //判断返回参数是否为空 if (null != resultByte && resultByte.length > 0) { String jsons = new String(resultByte, "UTF-8"); System.out.println(jsons); JSONObject json = new JSONObject(jsons); //json解析phoneNumber值 String phoneNumber = json.getString("phoneNumber"); System.out.println("phoneNumber:" + phoneNumber); return result; } return result.error("session_key:失败"); }