diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/aspect/ApiCryptoAsymmetricAop.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/aspect/ApiCryptoAsymmetricAop.java index e10b17f..e0477f6 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/aspect/ApiCryptoAsymmetricAop.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/aspect/ApiCryptoAsymmetricAop.java @@ -120,36 +120,7 @@ public class ApiCryptoAsymmetricAop { // 1. 拆解请求数据 // request 解密 if (annotation.enable() && annotation.requestDecrypt()){ - 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 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); - } - } - } + enterDecrypt(args, method, asymmetric, cryptoModel); } // 2. 执行方法 @@ -158,24 +129,7 @@ public class ApiCryptoAsymmetricAop { // 3. 返回响应数据 // response 加密 if (annotation.enable() && annotation.responseEncrypt()){ - if(returnValue != null){ - try { - // 执行加密过程 - if(returnValue instanceof ResultVo){ - ResultVo ret = (ResultVo) 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); - } - } + returnValue = resultEncrypt(returnValue, asymmetric, cryptoModel); } return returnValue; @@ -186,6 +140,77 @@ public class ApiCryptoAsymmetricAop { 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 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 ret = (ResultVo) 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; + } }