java 微信小程序獲取sessionkey並解碼獲取用戶信息

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.HashMap;
import java.util.Map;

@Component
public class Wechat {
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.appsecret}")
    private String appsecret;

    /**
     * 根據code獲取sessionkey和openid
     * @param code
     * @return
     */
    public Map<String, Object> sessionKey(String code)
    {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appsecret+"&js_code="+code+"&grant_type=authorization_code";
        Map<String, Object> wres = new HashMap<String, Object>();
        try {
            JSONObject res = HttpTools.doHttpGet(url);
            if (res.has("openid")) {
                String openid = res.getString("openid");
                String sessionKey = res.getString("session_key");
                wres.put("status", 0);
                wres.put("openid", openid);
                wres.put("sessionKey", sessionKey);
                return wres;
            }
            wres.put("status", 1);
            wres.put("msg", res.getString("errmsg"));
            return wres;
        } catch (JSONException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        }
    }

    /**
     * 根據encryptedData,iv,sessionKey解密獲取用戶信息
     * @param encryptedData
     * @param iv
     * @param sessionKey
     * @return
     */
    public Map<String, Object> decryptData(String encryptedData, String iv, String sessionKey)
    {
        Map<String, Object> wres = new HashMap<String, Object>();
        if (sessionKey.length() != 24) {
            wres.put("status", 1);
            wres.put("msg", "參數錯誤");
            return wres;
        }
        if (iv.length() != 24) {
            wres.put("status", 1);
            wres.put("msg", "參數錯誤");
            return wres;
        }
        byte[] aesKey = Base64.decode(sessionKey);
        byte[] aesIV = Base64.decode(iv);
        byte[] aesEncryptedData = Base64.decode(encryptedData);

        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(aesIV);
            cipher.init(cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] original = cipher.doFinal(aesEncryptedData);
            if (null != original && original.length > 0) {
                String result = new String(original, "UTF-8");
                JSONObject userInfo = new JSONObject(result);
                if (userInfo.has("openId")) {
                    wres.put("status", 0);
                    wres.put("openid", userInfo.getString("openId"));
                    wres.put("nickName", userInfo.getString("nickName"));
                    wres.put("gender", userInfo.getString("gender"));
                    wres.put("avatarUrl", userInfo.getString("avatarUrl"));
                    return wres;
                }
            }
            wres.put("status", 1);
            wres.put("msg", "解密錯誤");
            return wres;
        } catch (UnsupportedEncodingException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (NoSuchPaddingException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (NoSuchAlgorithmException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (InvalidAlgorithmParameterException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (InvalidKeyException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (BadPaddingException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (IllegalBlockSizeException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        } catch (JSONException ex) {
            wres.put("status", 1);
            wres.put("msg", ex.getMessage());
            return wres;
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章