当前位置:   article > 正文

java 微信小程序获取sessionkey并解码获取用户信息_public function decryptdata(string $sessionkey, st

public function decryptdata(string $sessionkey, string $iv, string $encrypte
  1. import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
  2. import org.codehaus.jettison.json.JSONException;
  3. import org.codehaus.jettison.json.JSONObject;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import javax.crypto.BadPaddingException;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.IllegalBlockSizeException;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.spec.IvParameterSpec;
  11. import javax.crypto.spec.SecretKeySpec;
  12. import java.io.UnsupportedEncodingException;
  13. import java.security.InvalidAlgorithmParameterException;
  14. import java.security.InvalidKeyException;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.security.spec.AlgorithmParameterSpec;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. @Component
  20. public class Wechat {
  21. @Value("${wechat.appid}")
  22. private String appid;
  23. @Value("${wechat.appsecret}")
  24. private String appsecret;
  25. /**
  26. * 根据code获取sessionkey和openid
  27. * @param code
  28. * @return
  29. */
  30. public Map<String, Object> sessionKey(String code)
  31. {
  32. String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appsecret+"&js_code="+code+"&grant_type=authorization_code";
  33. Map<String, Object> wres = new HashMap<String, Object>();
  34. try {
  35. JSONObject res = HttpTools.doHttpGet(url);
  36. if (res.has("openid")) {
  37. String openid = res.getString("openid");
  38. String sessionKey = res.getString("session_key");
  39. wres.put("status", 0);
  40. wres.put("openid", openid);
  41. wres.put("sessionKey", sessionKey);
  42. return wres;
  43. }
  44. wres.put("status", 1);
  45. wres.put("msg", res.getString("errmsg"));
  46. return wres;
  47. } catch (JSONException ex) {
  48. wres.put("status", 1);
  49. wres.put("msg", ex.getMessage());
  50. return wres;
  51. }
  52. }
  53. /**
  54. * 根据encryptedData,iv,sessionKey解密获取用户信息
  55. * @param encryptedData
  56. * @param iv
  57. * @param sessionKey
  58. * @return
  59. */
  60. public Map<String, Object> decryptData(String encryptedData, String iv, String sessionKey)
  61. {
  62. Map<String, Object> wres = new HashMap<String, Object>();
  63. if (sessionKey.length() != 24) {
  64. wres.put("status", 1);
  65. wres.put("msg", "参数错误");
  66. return wres;
  67. }
  68. if (iv.length() != 24) {
  69. wres.put("status", 1);
  70. wres.put("msg", "参数错误");
  71. return wres;
  72. }
  73. byte[] aesKey = Base64.decode(sessionKey);
  74. byte[] aesIV = Base64.decode(iv);
  75. byte[] aesEncryptedData = Base64.decode(encryptedData);
  76. try {
  77. SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, "AES");
  78. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  79. AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(aesIV);
  80. cipher.init(cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
  81. byte[] original = cipher.doFinal(aesEncryptedData);
  82. if (null != original && original.length > 0) {
  83. String result = new String(original, "UTF-8");
  84. JSONObject userInfo = new JSONObject(result);
  85. if (userInfo.has("openId")) {
  86. wres.put("status", 0);
  87. wres.put("openid", userInfo.getString("openId"));
  88. wres.put("nickName", userInfo.getString("nickName"));
  89. wres.put("gender", userInfo.getString("gender"));
  90. wres.put("avatarUrl", userInfo.getString("avatarUrl"));
  91. return wres;
  92. }
  93. }
  94. wres.put("status", 1);
  95. wres.put("msg", "解密错误");
  96. return wres;
  97. } catch (UnsupportedEncodingException ex) {
  98. wres.put("status", 1);
  99. wres.put("msg", ex.getMessage());
  100. return wres;
  101. } catch (NoSuchPaddingException ex) {
  102. wres.put("status", 1);
  103. wres.put("msg", ex.getMessage());
  104. return wres;
  105. } catch (NoSuchAlgorithmException ex) {
  106. wres.put("status", 1);
  107. wres.put("msg", ex.getMessage());
  108. return wres;
  109. } catch (InvalidAlgorithmParameterException ex) {
  110. wres.put("status", 1);
  111. wres.put("msg", ex.getMessage());
  112. return wres;
  113. } catch (InvalidKeyException ex) {
  114. wres.put("status", 1);
  115. wres.put("msg", ex.getMessage());
  116. return wres;
  117. } catch (BadPaddingException ex) {
  118. wres.put("status", 1);
  119. wres.put("msg", ex.getMessage());
  120. return wres;
  121. } catch (IllegalBlockSizeException ex) {
  122. wres.put("status", 1);
  123. wres.put("msg", ex.getMessage());
  124. return wres;
  125. } catch (JSONException ex) {
  126. wres.put("status", 1);
  127. wres.put("msg", ex.getMessage());
  128. return wres;
  129. }
  130. }
  131. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/388070?site
推荐阅读
相关标签
  

闽ICP备14008679号