|
@@ -0,0 +1,81 @@
|
|
|
+package com.zhentao.tool;
|
|
|
+
|
|
|
+import com.zhentao.domain.WechatAuthResponse;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+public class WechatLoginUtil {
|
|
|
+
|
|
|
+ private static final String APP_ID = "wxf1e39756b564fd41"; // 替换为你的AppID
|
|
|
+ private static final String APP_SECRET = "628082cc4dc055f61be83dffa6761e55"; // 替换为你的AppSecret
|
|
|
+ private static final String WECHAT_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+
|
|
|
+ public static WechatAuthResponse getAuthInfo(String code) throws IOException {
|
|
|
+ // 构造微信登录接口的URL,包含必要的参数
|
|
|
+ String url = WECHAT_LOGIN_URL + "?appid=" + APP_ID + "&secret=" + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
|
|
|
+
|
|
|
+ // 创建一个默认的HTTP客户端,用于发送HTTP请求
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ // 创建一个HTTP GET请求对象,指定请求的URL
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+
|
|
|
+ // 执行HTTP GET请求,获取微信服务器的响应
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
+
|
|
|
+ // 从响应中获取响应体内容,并将其转换为字符串
|
|
|
+ String jsonResponse = EntityUtils.toString(response.getEntity());
|
|
|
+ // 创建一个Jackson的ObjectMapper对象,用于处理JSON数据
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ // 将微信返回的JSON字符串解析为WechatAuthResponse对象
|
|
|
+ // WechatAuthResponse是一个自定义的Java类,用于封装微信返回的用户认证信息
|
|
|
+ return objectMapper.readValue(jsonResponse, WechatAuthResponse.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
|
|
|
+
|
|
|
+ public static String decryptPhoneNumber(String encryptedData, String sessionKey, String iv) throws Exception {
|
|
|
+ // Base64解码
|
|
|
+ byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
|
|
|
+ byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey);
|
|
|
+ byte[] ivBytes = Base64.getDecoder().decode(iv);
|
|
|
+
|
|
|
+ // 设置AES密钥和初始化向量
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyBytes, "AES");
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
|
+
|
|
|
+ // 创建Cipher实例并初始化
|
|
|
+ Cipher cipher = Cipher.getInstance(ALGORITHM);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
+
|
|
|
+ // 解密
|
|
|
+ byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes);
|
|
|
+ // 将解密后的字节转换为字符串
|
|
|
+ return new String(decryptedBytes, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|