优化 非对称加密 50%(未完)

v1.4.1
Parker 4 years ago
parent a8177cf1dd
commit fe1cdc5626

@ -120,36 +120,7 @@ public class ApiCryptoAsymmetricAop {
// 1. 拆解请求数据 // 1. 拆解请求数据
// request 解密 // request 解密
if (annotation.enable() && annotation.requestDecrypt()){ if (annotation.enable() && annotation.requestDecrypt()){
for (int i = 0; i < args.length; i++) { enterDecrypt(args, method, asymmetric, cryptoModel);
Object arg = args[i];
// 参数校验
if(arg instanceof BaseEncrypt){
// 获得加密数据
BaseEncrypt baseEncrypt = (BaseEncrypt) arg;
String encryptData = baseEncrypt.getEncryptData();
// 解密对象
Object dataToObj = asymmetric.decryptToObj(cryptoModel, encryptData);
// 根据方法类型转化对象
Type type = TypeUtil.getParamType(method, i);
Object obj = Convert.convert(type, dataToObj);
// 修改缓存中设备数据 空值不覆盖
Map<String, Object> modelBeanMap = BeanUtil.beanToMap(obj);
modelBeanMap.entrySet().removeIf(entry -> entry.getValue() == null);
// 反射赋值
Field[] fields = ReflectUtil.getFields(arg.getClass());
for (Field f : fields) {
Object val = modelBeanMap.get(f.getName());
if(val == null){
continue;
}
//根据需要,将相关属性赋上默认值
BeanUtil.setProperty(arg, f.getName(), val);
}
}
}
} }
// 2. 执行方法 // 2. 执行方法
@ -158,24 +129,7 @@ public class ApiCryptoAsymmetricAop {
// 3. 返回响应数据 // 3. 返回响应数据
// response 加密 // response 加密
if (annotation.enable() && annotation.responseEncrypt()){ if (annotation.enable() && annotation.responseEncrypt()){
if(returnValue != null){ returnValue = resultEncrypt(returnValue, asymmetric, cryptoModel);
try {
// 执行加密过程
if(returnValue instanceof ResultVo){
ResultVo<Object> ret = (ResultVo<Object>) returnValue;
ret.setData(
asymmetric.encrypt(cryptoModel, ret.getData())
);
returnValue = ret;
}else {
returnValue = asymmetric.encrypt(cryptoModel, returnValue);
CryptoAsymmetricUtil.encrypt(returnValue);
}
}catch (Exception e){
// 非对称加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_CRYPTO_EN);
}
}
} }
return returnValue; return returnValue;
@ -186,6 +140,77 @@ public class ApiCryptoAsymmetricAop {
return returnValue; return returnValue;
} }
// ===============================
/**
*
* @param args
* @param method
* @param asymmetric
* @param cryptoModel
*/
private void enterDecrypt(Object[] args, Method method, CryptoAsymmetricService asymmetric, CryptoAsymmetric cryptoModel) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
// 参数校验
if(arg instanceof BaseEncrypt){
// 获得加密数据
BaseEncrypt baseEncrypt = (BaseEncrypt) arg;
String encryptData = baseEncrypt.getEncryptData();
// 解密对象
Object dataToObj = asymmetric.decryptToObj(cryptoModel, encryptData);
// 根据方法类型转化对象
Type type = TypeUtil.getParamType(method, i);
Object obj = Convert.convert(type, dataToObj);
// 修改缓存中设备数据 空值不覆盖
Map<String, Object> modelBeanMap = BeanUtil.beanToMap(obj);
modelBeanMap.entrySet().removeIf(entry -> entry.getValue() == null);
// 反射赋值
Field[] fields = ReflectUtil.getFields(arg.getClass());
for (Field f : fields) {
Object val = modelBeanMap.get(f.getName());
if(val == null){
continue;
}
//根据需要,将相关属性赋上默认值
BeanUtil.setProperty(arg, f.getName(), val);
}
}
}
}
/**
*
* @param returnValue
* @param asymmetric
* @param cryptoModel
* @return Object
*/
private Object resultEncrypt(Object returnValue, CryptoAsymmetricService asymmetric, CryptoAsymmetric cryptoModel) {
if(returnValue != null){
try {
// 执行加密过程
if(returnValue instanceof ResultVo){
// 重新赋值 data
ResultVo<Object> ret = (ResultVo<Object>) returnValue;
ret.setData(
asymmetric.encrypt(cryptoModel, ret.getData())
);
returnValue = ret;
}else {
returnValue = asymmetric.encrypt(cryptoModel, returnValue);
CryptoAsymmetricUtil.encrypt(returnValue);
}
}catch (Exception e){
// 非对称加密失败
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_CRYPTO_EN);
}
}
return returnValue;
}
} }

Loading…
Cancel
Save