美文网首页
Java 后台获取小程序 openId 和 unionId

Java 后台获取小程序 openId 和 unionId

作者: panzhangbao | 来源:发表于2019-01-24 11:27 被阅读12次
    package com.simple.business.service.app;
    
    import com.simple.business.gateway.UserGateway;
    import com.simple.common.config.CommonConstant;
    import com.simple.common.utils.HttpClientUtils;
    import com.simple.common.utils.JSONUtils;
    import jodd.util.Base64;
    import org.apache.commons.lang.StringUtils;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.AlgorithmParameters;
    import java.security.Security;
    import java.util.Arrays;
    import java.util.Map;
    
    /**
     * 微信 service
     *
     * Created by panzhangbao on 2018-12-26 13:39:28
     * Copyright © 2018 panzhangbao. All rights reserved.
     */
    @Service
    public class WechatService {
        /**
         * 获取小程序 openId
         *      openid 用户唯一标识
         *      session_key 会话秘钥
         *      unionid 需满足 unionId 下发条件
         *
         *  UnionID机制说明
         *      如果开发者拥有多个移动应用、网站应用、和公众帐号(包括小程序),可通过unionid来区分用户的唯一性,
         *      因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号(包括小程序),用户的 unionid 是唯一的。
         *      换句话说,同一用户,对同一个微信开放平台下的不同应用,unionid 是相同的。
         *
         * @param code 微信 code
         * @return java.util.Map<java.lang.String, java.lang.String>
         * @date 2018/12/26 21:31
         * @author panzhangbao
         */
        private Map<String, String> getAppletOpenId(String code) {
            if (StringUtils.isBlank(code)) {
                return null;
            }
    
            String url = "https://api.weixin.qq.com/sns/jscode2session?"
                    + "appid=" + CommonConstant.APPLET_APP_ID
                    + "&secret=" + CommonConstant.APPLET_SECRET
                    + "&js_code=" + code + "&grant_type=authorization_code";
            try {
                return JSONUtils.jsonToMap(HttpClientUtils.get(url));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        /**
         * 小程序解密用户数据
         * @param sessionKey 会话秘钥
         * @param encryptedData 被加密的数据
         * @param iv 偏移量
         * @return java.util.Map
         * @date 2019/1/24 10:39
         * @author panzhangbao
         */
        public Map decryptionUserInfo(String sessionKey, String encryptedData, String iv) {
            // 会话秘钥
            byte[] sessionKeyByte = Base64.decode(sessionKey);
            // 被加密的数据
            byte[] dataByte = Base64.decode(encryptedData);
            // 偏移量
            byte[] ivByte = Base64.decode(iv);
    
            try {
                // 如果密钥不足16位,那么就补足. 这个 if 中的内容很重要
                int base = 16;
                if (sessionKeyByte.length % base != 0) {
                    int groups = sessionKeyByte.length / base + (sessionKeyByte.length % base != 0 ? 1 : 0);
                    byte[] temp = new byte[groups * base];
                    Arrays.fill(temp, (byte) 0);
                    System.arraycopy(sessionKeyByte, 0, temp, 0, sessionKeyByte.length);
                    sessionKeyByte = temp;
                }
                // 初始化
                Security.addProvider(new BouncyCastleProvider());
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
                SecretKeySpec spec = new SecretKeySpec(sessionKeyByte, "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");
                    return JSONUtils.jsonToMap(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Java 后台获取小程序 openId 和 unionId

          本文链接:https://www.haomeiwen.com/subject/dudyjqtx.html