因为本人是后端,所以前端的知识不太懂,使用hutool工具包非常的简单易懂易操作,不管是临时的需求还是深入了解都有很大的帮助,话不多说,直接上代码!!!
微信授权文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html**
- 引入hutoo依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>版本自定义选择</version>
</dependency>
- yml配置
weChat:
#微信请求接口
sessionHost: https://api.weixin.qq.com/sns/jscode2session?
#小程序appId
appId: *******
#小程序appSecret
secret: ******
- 微信授权配置类(可以写成一个公共的授权utils)
* @Description: 获取授权码
* @return: JSONObject (cn.hutool.json 这个包,从JSONObject中获取对应的value就可以了)
* @Author: Gyan
public static JSONObject authCode2Session(String code) {
Map<String, Object> requestURL = new HashMap<>();
//小程序appId
requestURL.put("appid", appId);
//小程序 appSecret
requestURL.put("secret", secret);
//这个是前端请求微信的接口获取,后端不需要管是什么东西
requestURL.put("js_code", code);
//这个是微信默认,不需要更改
requestURL.put("grant_type", "authorization_code");
JSONObject jsonObject = null;
try {
String result = HttpUtil.get(sessionHost, requestURL);
jsonObject = JSONUtil.parseObj(result);
} catch (Exception e) {
log.error("调用微信接口失败", e, e.getMessage());
e.printStackTrace();
}
return jsonObject;
}
- 手机授权配置类
* @Description: 手机授权
* @param: encryptedData 加密数据(前端传)
* @param: iv 加密算法的初始向量(前端传)
* @param: sessionKey (第一步微信授权通过code会获取到)
* @return: JSONObject(cn.hutool.json)
* @Author: Gyan
public static JSONObject getUserInfo(String encryptedData, String iv, String sessionKey) {
// 被加密的数据
byte[] dataByte = cn.hutool.core.codec.Base64.decode(encryptedData);
// 加密秘钥
byte[] keyByte = cn.hutool.core.codec.Base64.decode(sessionKey);
// 偏移量
byte[] ivByte = cn.hutool.core.codec.Base64.decode(iv);
try {
// 如果密钥不足16位,那么就补足.
int base = 16;
keyByte = completToBase(keyByte, base);
ivByte = completToBase(ivByte, base);
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
SecretKeySpec spec = new SecretKeySpec(keyByte, "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");
System.out.println(result);
return JSONUtil.parseObj(result);
}
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
} catch (NoSuchPaddingException e) {
log.error(e.getMessage(), e);
} catch (InvalidParameterSpecException e) {
log.error(e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
log.error(e.getMessage(), e);
} catch (BadPaddingException e) {
log.error(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
} catch (InvalidKeyException e) {
log.error(e.getMessage(), e);
} catch (InvalidAlgorithmParameterException e) {
log.error(e.getMessage(), e);
} catch (NoSuchProviderException e) {
log.error(e.getMessage(), e);
}
return null;
}
//补全数组位数
public static byte[] completToBase(byte[] bytes, int base) {
byte[] temp = new byte[]{};
if (bytes.length % base != 0) {
int groups = bytes.length / base + (bytes.length % base != 0 ? 1 : 0);
temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(bytes, 0, temp, 0, bytes.length);
return temp;
} else {
return bytes;
}
}
就这样简单的四步完成微信+手机号码授权!足以应付突如其来的需求!再根据实际业务加入其他的业务逻辑!seeBye!