Java-微信授权and手机号授权

因为本人是后端,所以前端的知识不太懂,使用hutool工具包非常的简单易懂易操作,不管是临时的需求还是深入了解都有很大的帮助,话不多说,直接上代码!!!

微信授权文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html**

  1. 引入hutoo依赖
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>版本自定义选择</version>
		</dependency>

  1. yml配置
weChat:
  #微信请求接口
  sessionHost: https://api.weixin.qq.com/sns/jscode2session?
  #小程序appId 
  appId: *******
  #小程序appSecret
  secret: ******
  1. 微信授权配置类(可以写成一个公共的授权utils)

     * @Description: 获取授权码
     * @return: JSONObject (cn.hutool.json 这个包,JSONObject中获取对应的value就可以了)
     * @Author: Gyan
    public static JSONObject authCode2Session(String code) {
        Map<String, Object> requestURL = new HashMap<>();
        //小程序appId
        requestURL.put("appid", appId);
        //小程序 appSecret
        requestURL.put("secret", secret);
        //这个是前端请求微信的接口获取,后端不需要管是什么东西
        requestURL.put("js_code", code);
        //这个是微信默认,不需要更改
        requestURL.put("grant_type", "authorization_code");

        JSONObject jsonObject = null;
        try {
            String result = HttpUtil.get(sessionHost, requestURL);
            jsonObject = JSONUtil.parseObj(result);
        } catch (Exception e) {
            log.error("调用微信接口失败", e, e.getMessage());
            e.printStackTrace();
        }
        return jsonObject;
    }
  1. 手机授权配置类
	 * @Description: 手机授权
	 * @param: encryptedData 加密数据(前端传)
	 * @param: iv 加密算法的初始向量(前端传)
	 * @param: sessionKey (第一步微信授权通过code会获取到)
     * @return: JSONObject(cn.hutool.json)
     * @Author: Gyan
     public static JSONObject getUserInfo(String encryptedData, String iv, String sessionKey) {
        // 被加密的数据
        byte[] dataByte = cn.hutool.core.codec.Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = cn.hutool.core.codec.Base64.decode(sessionKey);
        // 偏移量
        byte[] ivByte = cn.hutool.core.codec.Base64.decode(iv);
        try {
            // 如果密钥不足16位,那么就补足.
            int base = 16;
            keyByte = completToBase(keyByte, base);
            ivByte = completToBase(ivByte, base);

            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, "UTF-8");
                System.out.println(result);
                return JSONUtil.parseObj(result);
            }
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage(), e);
        } catch (NoSuchPaddingException e) {
            log.error(e.getMessage(), e);
        } catch (InvalidParameterSpecException e) {
            log.error(e.getMessage(), e);
        } catch (IllegalBlockSizeException e) {
            log.error(e.getMessage(), e);
        } catch (BadPaddingException e) {
            log.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            log.error(e.getMessage(), e);
        } catch (InvalidAlgorithmParameterException e) {
            log.error(e.getMessage(), e);
        } catch (NoSuchProviderException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
//补全数组位数
    public static byte[] completToBase(byte[] bytes, int base) {
        byte[] temp = new byte[]{};
        if (bytes.length % base != 0) {
            int groups = bytes.length / base + (bytes.length % base != 0 ? 1 : 0);
            temp = new byte[groups * base];
            Arrays.fill(temp, (byte) 0);
            System.arraycopy(bytes, 0, temp, 0, bytes.length);
            return temp;
        } else {
            return bytes;
        }
    }

就这样简单的四步完成微信+手机号码授权!足以应付突如其来的需求!再根据实际业务加入其他的业务逻辑!seeBye!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值