/** * 对象隐私数据加解密工具类. * * @author d * @since 2018年05月22日 */ public final class PrivacyDataHandler { private static ConcurrentHashMap<String, List<PrivacyDataVo>> encryptMap = new ConcurrentHashMap<>(); private static final Logger logger = LoggerFactory.getLogger(PrivacyDataHandler.class); /** * 获得对象中的加密域 * * @param object 对象 * @return 加密域 */ public static synchronized List<PrivacyDataVo> getEncryptList(Object object) { String key = object.getClass().toString(); if (null == encryptMap.get(key)) { fillEncryptMap(object); } List<PrivacyDataVo> field = new ArrayList<>(encryptMap.get(key)); return field; } /** * 过得加密列表 * * @param object 对象 * @return 加密列表 */ public static synchronized Map<String, List<PrivacyDataVo>> getEncryptListWithOldVer(Object object) { Map<String, List<PrivacyDataVo>> map = new HashMap<>(); List<PrivacyDataVo> cipherList = getEncryptList(object); Iterator<PrivacyDataVo> iter = cipherList.iterator(); List<PrivacyDataVo> clearList = new ArrayList<>(); List<PrivacyDataVo> ver2 = new ArrayList<>(); while (iter.hasNext()) { PrivacyDataVo vo = iter.next(); if ("0".equals(vo.getVersion())) { clearList.add(vo); iter.remove(); } if ("2".equals(vo.getVersion())) { ver2.add(vo); iter.remove(); } } // 之前未加密 map.put("VER_0", clearList); // 之前用ECB方式加密 map.put("VER_1", cipherList); // 之前用ECB方式加密,现在为空不处理 map.put("VER_2", ver2); return map; } /** * 获取实体所有注解字段 * * @param object 对象 */ private static void fillEncryptMap(Object object) { String key = object.getClass().toString(); List<PrivacyDataVo> field = new ArrayList<>(); Class<?> aClass = object.getClass(); Field[] fields = aClass.getDeclaredFields(); for (Field srcField : fields) { PrivacyData privacyData = srcField.getAnnotation(PrivacyData.class); if (null == privacyData) { continue; } String srcName = srcField.getName(); if (privacyData.value()) { PrivacyDataVo vo = new PrivacyDataVo(); vo.setValue(true); vo.setFieldName(srcName); vo.setVersion(privacyData.version()); vo.setCharset(privacyData.charset()); field.add(vo); } } encryptMap.put(key, field); } /** * 获得加解密域的值 * * @param object 对象 * @param field 字段 * @return 加解密域的值 */ public static List<String> getOriginalTextList(Object object, List<PrivacyDataVo> field) { if (null == field) { return null; } List<String> values = new ArrayList<>(); Iterator<PrivacyDataVo> iter = field.iterator(); while (iter.hasNext()) { PrivacyDataVo vo = iter.next(); String fieldName = vo.getFieldName(); String fieldValue = (String) getFieldValue(object, fieldName); if (StringUtils.isEmpty(fieldValue)) { iter.remove(); continue; } values.add(fieldValue); } return values; } /** * 设置隐私字段值 * * @param object 待处理的对象 * @param field object带有PrivacyData的字段 * @param value 隐私数据待替换的值 * @param excludedFields 带有PrivacyData不被替换的字段 * @param <T> 参数化类型 * @return 返回 */ public static <T> T setPrivacyDataFiledValue(T object, List<PrivacyDataVo> field, String value, String[] excludedFields) { if (null == field) { return null; } List<String> excludedFieldList = null; if (null != excludedFields) { excludedFieldList = Arrays.asList(excludedFields); } Iterator<PrivacyDataVo> iter = field.iterator(); while (iter.hasNext()) { PrivacyDataVo vo = iter.next(); String fieldName = vo.getFieldName(); if (null != excludedFieldList && excludedFieldList.contains(fieldName)) { continue; } // 加密数据设置空 setFieldValue(object, fieldName, value); } return object; } /** * 设置加解密值到原对象 * * @param object 对象 * @param field 字段 * @param sksResult SKS结果 */ public static void dealSksResult(Object object, List<PrivacyDataVo> field, List<String> sksResult) { for (int i = 0; i < field.size(); i++) { String fieldName = field.get(i).getFieldName(); setFieldValue(object, fieldName, sksResult.get(i)); } } /** * 获得Obj对象的fieldName属性的值 * * @param obj 对象 * @param fieldName 字段 * @return 返回值 */ public static Object getFieldValue(Object obj, String fieldName) { Object value = null; if (null == obj) { return value; } try { value = PropertyUtils.getProperty(obj, fieldName); } catch (Exception e) { logger.error("getFieldValue failed: ", e.getLocalizedMessage(), e); } return value; } /** * 设置Obj对象的fieldName属性的值 * * @param obj 对象 * @param fieldName 字段名 * @param value 值 */ private static void setFieldValue(Object obj, String fieldName, String value) { if (null == obj) { return; } try { PropertyUtils.setProperty(obj, fieldName, value); } catch (Exception e) { logger.error("setFieldValue failed: ", e.getLocalizedMessage(), e); } } }
解密调用
List<PrivacyDataVo> privacyDataVoList = PrivacyDataHandler.getEncryptList(object); List<String> originalTextList = PrivacyDataHandler.getOriginalTextList(object, privacyDataVoList);
加密调用
List<PrivacyDataVo> privacyDataVos = PrivacyDataHandler.getEncryptList(temp); T clearedObject = PrivacyDataHandler.setPrivacyDataFiledValue(temp, privacyDataVos, null, excludedFields);
标签:方式,List,object,加解密,param,vo,field,fieldName,注解 From: https://www.cnblogs.com/dukedu/p/16855710.html