以上爲一個同事寫的關於脫敏的一段代碼 ,這個關於脫敏的需求我感受是一段很是完美的關於「反射」的需求的應用場景,適合初學者學習前端
很簡單 ,安所有門要求,針對客戶的基礎信息數據例如手機號、身份證、姓名等信息進行脫敏,防止客戶信息外泄,其中主要包括兩個部分java
接口和日誌。spring
針對接口部分,我公司進行先後端分離的操做,前端和後端的交互大部分以json交互爲主,springmvc能夠講java對象轉成成apache
json格式,大部分傳給前端的都是json對象,因而乎,個人同事就將脫敏接口的動做概括爲脫敏對象的動做, 可是先後端接口json
有幾百個,涉及的類更是不只期數,不可能針對沒一個返回接口的對象都作一些從新定義值的處理 ,因而反射應運而生後端
該同事 寫了一個脫敏的工具類(SensitiveFormatter),包含兩種方法安全
public static void formatObjectByAttributes(Object o, int type, String... attributes) {
private static void setValue(Field f, Object o, int type) {
mvc
針對須要脫敏的對象和字段出直接調用該脫敏類 app
appDao爲脫敏的對象,SensitiveFormatter.NAME爲脫敏的類型 ,Name,爲脫敏的變量名稱前後端分離
SensitiveFormatter.formatObjectByAttributes(appDao, SensitiveFormatter.NAME, "Name");
formatObjectByAttributes 方法,對該對象進行反射,或者到與Name對應的Field
{java.lang.reflect.Field
爲咱們提供了獲取當前對象的成員變量的類型,和從新設值的方法},
Field field = o.getClass().getDeclaredField(attribute);
·而後在這個File重新進行設置,調用的正式setValue方法
String name = (String) f.get(o); if (StringUtils.isNotBlank(name) && name.length() > 1) { name = name.charAt(0) + name.substring(1).replaceAll("[^x00-xff]|\\w", "*"); f.set(o, name); }
代碼和原理很簡單 ,一下是所有代碼,能夠做爲學習的參考
package com.paic.common.utils; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.lang.reflect.Field; import java.util.List; import java.util.Map; public class SensitiveFormatter { private static Log logger = LogFactory.getLog(SensitiveFormatter.class); public static final int NAME = 1; public static final int PHONE = 2; public static final int ID_NO = 3; public static final int SEX = 4; /** * 參數脫敏處理List數據 * 反射處理POJO * * @param list 脫敏List對象 * @param type 脫敏類型 * @param attributes 脫敏成員變量名稱 * @throws Exception */ public static void formatListByAttributes(List list, int type, String... attributes) { if (attributes == null || attributes.length <= 0) { return; } if (list != null && list.size() > 0) { for (String attribute : attributes) { try { Field field = list.get(0).getClass().getDeclaredField(attribute); for (Object o : list) { setValue(field, o, type); } } catch (Exception e) { logger.error("SensitiveFormatter --- NoSuchFieldException : " + e.getMessage()); } } } } /** * 參數脫敏處理Object數據 * 反射處理POJO * * @param o 脫敏對象 * @param type 脫敏類型 * @param attributes 脫敏成員變量名稱 * @throws Exception */ public static void formatObjectByAttributes(Object o, int type, String... attributes) { if (attributes == null || attributes.length <= 0) { return; } if (o != null) { for (String attribute : attributes) { try { Field field = o.getClass().getDeclaredField(attribute); setValue(field, o, type); } catch (Exception e) { logger.error("SensitiveFormatter --- NoSuchFieldException : " + e.getMessage()); } } } } /** * 參數脫敏處理List<Map>數據 * * @param list 脫敏List<Map>對象 * @param type 脫敏類型 * @param attributes 脫敏成員變量名稱 * @throws Exception */ public static void formatListWithMapByAttributes(List<Map<String, Object>> list, int type, String... attributes) { if (attributes == null || attributes.length <= 0) { return; } if (list != null && list.size() > 0) { for (String attribute : attributes) { try { for (Map map : list) { setMapValue(map, attribute, type); } } catch (Exception e) { logger.error("SensitiveFormatter --- NoSuchFieldException : " + e.getMessage()); } } } } /** * 參數脫敏處理Map數據 * * @param map 脫敏Map對象 * @param type 脫敏類型 * @param attributes 脫敏成員變量名稱 * @throws Exception */ public static void formatListWithMapByAttributes(Map<String, Object> map, int type, String... attributes) { if (attributes == null || attributes.length <= 0) { return; } if (map != null) { for (String attribute : attributes) { try { setMapValue(map, attribute, type); } catch (Exception e) { logger.error("SensitiveFormatter --- NoSuchFieldException : " + e.getMessage()); } } } } /** * 註解脫敏處理list數據 * * @param list 脫敏List對象 * @throws Exception */ public static void formatListByAnnotation(List list) { if (list != null && list.size() > 0) { for (Field field : list.get(0).getClass().getDeclaredFields()) { //得到註解的對象 SensitiveFormat sensitiveFormat = field.getAnnotation(SensitiveFormat.class); if (sensitiveFormat != null) { for (Object o : list) { setValue(field, o, sensitiveFormat.type()); } } } } } /** * 註解脫敏處理object數據 * * @param o 脫敏對象 * @throws Exception */ public static void formatObjectByAnnotation(Object o) { if (o != null) { for (Field field : o.getClass().getDeclaredFields()) { //得到註解的對象 SensitiveFormat sensitiveFormat = field.getAnnotation(SensitiveFormat.class); if (sensitiveFormat != null) { setValue(field, o, sensitiveFormat.type()); } } } new SimplePropertyPreFilter(); } private static void setValue(Field f, Object o, int type) { System.out.println(o); f.setAccessible(true); try { switch (type) { case NAME: String name = (String) f.get(o); if (StringUtils.isNotBlank(name) && name.length() > 1) { name = name.charAt(0) + name.substring(1).replaceAll("[^x00-xff]|\\w", "*"); f.set(o, name); } break; case PHONE: f.set(o, CommonUtil.addCode((String) f.get(o), 3, 2)); break; case ID_NO: f.set(o, CommonUtil.addCode((String) f.get(o), 1, 1)); break; case SEX: if (f.getType().equals(String.class)) { f.set(o, "*"); } else { f.set(o, null); } default: break; } } catch (Exception e) { logger.error("SensitiveFormatter --- setValue --- occurs error : " + e.getMessage()); } } private static void setMapValue(Map<String, Object> map, String attributeName, int type) { try { switch (type) { case NAME: String name = (String) map.get(attributeName); if (StringUtils.isNotBlank(name) && name.length() > 1) { name = name.charAt(0) + name.substring(1).replaceAll("[^x00-xff]|\\w", "*"); map.put(attributeName, name); } break; case PHONE: map.put(attributeName, CommonUtil.addCode((String) map.get(attributeName), 3, 2)); break; case ID_NO: map.put(attributeName, CommonUtil.addCode((String) map.get(attributeName), 1, 1)); break; default: break; } } catch (Exception e) { logger.error("SensitiveFormatter --- setMapValue --- occurs error : " + e.getMessage()); } } public static void main(String[] args) { A a =new A(); a.setName("ss"); formatObjectByAttributes(a,1,"name"); System.out.println(a.getName()); } } class A{ String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }