WechatMiniUtils.java
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.glsx.common.utils.wehcat;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtils;
/**
* 微信小程序工具类
*/
public class WechatMiniUtils {
private static Logger logger = LoggerFactory.getLogger(WechatMiniUtils.class);
//public String GET_OPENID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$APPID$&secret=$SECRET$&code=$CODE$&grant_type=authorization_code";
/*
* 获取小程序的openid
*/
public static String GET_OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$APPID$&secret=$SECRET$&js_code=$JSCODE$&grant_type=authorization_code";
public static final String AES = "AES";
public static final String AES_CBC_PADDING = "AES/CBC/PKCS7Padding";
public static Map<String, Object> getWechatMiniOpenId(String appid, String secret, String code) throws Exception {
Map<String, Object> wto = new HashMap<String, Object>();
String url = GET_OPENID_URL.replace("$APPID$", appid).replace("$SECRET$", secret).replace("$JSCODE$", code);
HttpUtils httpUtils = new HttpUtils();
String result = httpUtils.get(url, null);
logger.info(url + " 获取小程序openId接口返回:" + result);
if (StringUtils.isNotEmpty(result)) {
JSONObject openIdJO = JSON.parseObject(result);
for (String key : openIdJO.keySet()) {
wto.put(key, openIdJO.getString(key));
}
}
return wto;
}
public static Map<String, Object> authPhone(String encryptedData, String session_key, String iv) {
Map<String, Object> map = new HashMap<>();
try {
String result = wxDecrypt(encryptedData, session_key, iv);
JSONObject json = JSONObject.parseObject(result);
if (json.containsKey("phoneNumber")) {
String phone = json.getString("phoneNumber");
// String appid =
// json.getJSONObject("watermark").getString("appid");
if (StringUtils.isNoneBlank(phone)) {
map.put("code", 0);
map.put("msg", "成功");
map.put("data", phone);
} else {
map.put("code", 4001);
map.put("msg", "失败!用户未绑定手机号");
}
} else {
map.put("code", 4001);
map.put("msg", "获取失败!");
}
} catch (Exception e) {
map.put("code", 4001);
map.put("msg", "获取失败");
}
return map;
}
/**
* * 微信 数据解密<br/>
* * 对称解密使用的算法为 AES-128-CBC,数据采用PKCS#7填充<br/>
* * 对称解密的目标密文:encrypted=Base64_Decode(encryptData)<br/>
* * 对称解密秘钥:key = Base64_Decode(session_key),aeskey是16字节<br/>
* * 对称解密算法初始向量:iv = Base64_Decode(iv),同样是16字节<br/>
* * * @param encrypted 目标密文 * @param session_key 会话ID * @param iv 加密算法的初始向量
*
*/
public static String wxDecrypt(String encrypted, String session_key, String iv) {
String result = null;
byte[] encrypted64 = Base64.decodeBase64(encrypted);
byte[] key64 = Base64.decodeBase64(session_key);
byte[] iv64 = Base64.decodeBase64(iv);
try {
init();
result = new String(decrypt(encrypted64, key64, generateIV(iv64)));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* * 初始化密钥
*
*/
public static void init() throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator.getInstance(AES).init(128);
}
/**
* * 生成iv
*
*/
public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
// iv 为一个 16 字节的数组,这里采用和 iOS 端一样的构造方法,数据全为0
// Arrays.fill(iv, (byte) 0x00);
AlgorithmParameters params = AlgorithmParameters.getInstance(AES);
params.init(new IvParameterSpec(iv));
return params;
}
/**
* * 生成解密
*
*/
public static byte[] decrypt(byte[] encryptedData, byte[] keyBytes, AlgorithmParameters iv) throws Exception {
Key key = new SecretKeySpec(keyBytes, AES);
Cipher cipher = Cipher.getInstance(AES_CBC_PADDING);
// 设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return cipher.doFinal(encryptedData);
}
}