|
@@ -1,5 +1,7 @@
|
|
|
package com.ruoyi.web.controller.system;
|
|
|
|
|
|
+import java.security.spec.AlgorithmParameterSpec;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -7,7 +9,10 @@ import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.ruoyi.common.config.WxAppConfig;
|
|
|
import com.ruoyi.common.core.domain.model.WxLoginBody;
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
+
|
|
|
+
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -28,6 +33,10 @@ import com.ruoyi.framework.web.service.TokenService;
|
|
|
import com.ruoyi.system.service.ISysMenuService;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 登录验证
|
|
@@ -132,13 +141,59 @@ public class SysLoginController
|
|
|
//解密
|
|
|
String decryptResult="";
|
|
|
try {
|
|
|
-// decryptResult=decrypt(session_key,encryptedIv,encryptedData);
|
|
|
+ decryptResult=decrypt(session_key,encryptedIv,encryptedData);
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
return AjaxResult.error("微信登录失败");
|
|
|
}
|
|
|
- return AjaxResult.success();
|
|
|
+ if (StringUtils.hasText(decryptResult)){
|
|
|
+ //如果解析成功,获取token
|
|
|
+ String token=loginService.wxLogin(decryptResult);
|
|
|
+ AjaxResult ajax=AjaxResult.success();
|
|
|
+ ajax.put(Constants.TOKEN,token);
|
|
|
+ return ajax;
|
|
|
+ }else {
|
|
|
+ return AjaxResult.error("微信登录失败");
|
|
|
+ }
|
|
|
+// return AjaxResult.success();
|
|
|
+
|
|
|
|
|
|
+ }
|
|
|
|
|
|
+ private String decrypt(String session_key, String encryptedIv, String encryptedData) throws Exception {
|
|
|
+ //转化为字节数组
|
|
|
+ byte[] key = Base64.decodeBase64(session_key);
|
|
|
+ byte[] iv = Base64.decodeBase64(encryptedIv);
|
|
|
+ byte[] encData = Base64.decodeBase64(encryptedData);
|
|
|
+ //如果密钥不足16位,那么就补足
|
|
|
+ int base = 16;
|
|
|
+ if (key.length % base != 0){
|
|
|
+ int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
|
|
|
+ byte[] temp = new byte[groups * base];
|
|
|
+ Arrays.fill(temp, (byte) 0);
|
|
|
+ System.arraycopy(key, 0, temp, 0, key.length);
|
|
|
+ key = temp;
|
|
|
+ }
|
|
|
+ //如果初始向量不足16位,也补足
|
|
|
+ if (iv.length % base != 0){
|
|
|
+ int groups = iv.length / base + (iv.length % base != 0 ? 1 : 0);
|
|
|
+ byte[] temp = new byte[groups * base];
|
|
|
+ Arrays.fill(temp, (byte) 0);
|
|
|
+ System.arraycopy(iv, 0, temp, 0, iv.length);
|
|
|
+ iv = temp;
|
|
|
+ }
|
|
|
+ AlgorithmParameterSpec ivSpec=new IvParameterSpec(iv);
|
|
|
+ String resultStr=null;
|
|
|
+ try {
|
|
|
+ Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
+ SecretKeySpec keySpec=new SecretKeySpec(key, "AES");
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
+ resultStr=new String(cipher.doFinal(encData),"UTF-8");
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.info("解析错误!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //解析解密后的字符串
|
|
|
+ return resultStr;
|
|
|
}
|
|
|
}
|