註解demo

轉自 : http://blog.csdn.net/liuc0317/article/details/48787793



Java 中對自定義註解的說明請參見:

http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html

有這樣一個場景,系統中可以出現敏感的數據,在打印日誌的時候,我們並不希望打印出現,這樣,我們使用自己定義註解,來解決這個問題。

定義需要脫敏的字段規則。

[java] view plain copy
  1. import java.lang.reflect.Array;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4. import java.util.Collection;  
  5. import java.util.HashSet;  
  6. import java.util.Iterator;  
  7. import java.util.Map;  
  8. import java.util.Map.Entry;  
  9. import java.util.Set;  
  10.   
  11. import org.apache.commons.lang.ArrayUtils;  
  12. import org.apache.commons.lang.StringUtils;  
  13.   
  14. import com.alibaba.fastjson.JSON;  
  15. import com.alibaba.fastjson.serializer.SerializerFeature;  
  16. import com.google.gson.Gson;  
  17. import com.ucf.platform.framework.core.annotation.SensitiveInfo;  
  18. import com.ucf.platform.framework.core.log.UcfLogger;  
  19. import com.ucf.platform.framework.core.log.UcfLoggerFactory;  
  20.   
  21. /** 
  22.  * @Title: SensitiveInfoUtils.java 
  23.  * @Copyright: Copyright (c) 2011 
  24.  * @Description: <br> 
  25.  *               敏感信息屏蔽工具<br> 
  26.   */  
  27. public final class SensitiveInfoUtils {  
  28.   
  29.     private final static UcfLogger logger = UcfLoggerFactory.getLogger(SensitiveInfoUtils.class);  
  30.   
  31.     /** 
  32.      * [中文姓名] 只顯示第一個漢字,其他隱藏爲2個星號<例子:李**> 
  33.      *  
  34.      * @param name 
  35.      * @return 
  36.      */  
  37.     public static String chineseName(String fullName) {  
  38.         if (StringUtils.isBlank(fullName)) {  
  39.             return "";  
  40.         }  
  41.         String name = StringUtils.left(fullName, 1);  
  42.         return StringUtils.rightPad(name, StringUtils.length(fullName), "*");  
  43.     }  
  44.   
  45.     /** 
  46.      * [中文姓名] 只顯示第一個漢字,其他隱藏爲2個星號<例子:李**> 
  47.      *  
  48.      * @param familyName 
  49.      * @param givenName 
  50.      * @return 
  51.      */  
  52.     public static String chineseName(String familyName, String givenName) {  
  53.         if (StringUtils.isBlank(familyName) || StringUtils.isBlank(givenName)) {  
  54.             return "";  
  55.         }  
  56.         return chineseName(familyName + givenName);  
  57.     }  
  58.   
  59.     /** 
  60.      * [身份證號] 顯示最後四位,其他隱藏。共計18位或者15位。<例子:*************5762> 
  61.      *  
  62.      * @param id 
  63.      * @return 
  64.      */  
  65.     public static String idCardNum(String id) {  
  66.         if (StringUtils.isBlank(id)) {  
  67.             return "";  
  68.         }  
  69.         String num = StringUtils.right(id, 4);  
  70.         return StringUtils.leftPad(num, StringUtils.length(id), "*");  
  71.     }  
  72.   
  73.     /** 
  74.      * [固定電話] 後四位,其他隱藏<例子:****1234> 
  75.      *  
  76.      * @param num 
  77.      * @return 
  78.      */  
  79.     public static String fixedPhone(String num) {  
  80.         if (StringUtils.isBlank(num)) {  
  81.             return "";  
  82.         }  
  83.         return StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*");  
  84.     }  
  85.   
  86.     /** 
  87.      * [手機號碼] 前三位,後四位,其他隱藏<例子:138******1234> 
  88.      *  
  89.      * @param num 
  90.      * @return 
  91.      */  
  92.     public static String mobilePhone(String num) {  
  93.         if (StringUtils.isBlank(num)) {  
  94.             return "";  
  95.         }  
  96.         return StringUtils.left(num, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*"), "***"));  
  97.     }  
  98.   
  99.     /** 
  100.      * [地址] 只顯示到地區,不顯示詳細地址;我們要對個人信息增強保護<例子:北京市海淀區****> 
  101.      *  
  102.      * @param address 
  103.      * @param sensitiveSize 
  104.      *            敏感信息長度 
  105.      * @return 
  106.      */  
  107.     public static String address(String address, int sensitiveSize) {  
  108.         if (StringUtils.isBlank(address)) {  
  109.             return "";  
  110.         }  
  111.         int length = StringUtils.length(address);  
  112.         return StringUtils.rightPad(StringUtils.left(address, length - sensitiveSize), length, "*");  
  113.     }  
  114.   
  115.     /** 
  116.      * [電子郵箱] 郵箱前綴僅顯示第一個字母,前綴其他隱藏,用星號代替,@及後面的地址顯示<例子:g**@163.com> 
  117.      *  
  118.      * @param email 
  119.      * @return 
  120.      */  
  121.     public static String email(String email) {  
  122.         if (StringUtils.isBlank(email)) {  
  123.             return "";  
  124.         }  
  125.         int index = StringUtils.indexOf(email, "@");  
  126.         if (index <= 1)  
  127.             return email;  
  128.         else  
  129.             return StringUtils.rightPad(StringUtils.left(email, 1), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email)));  
  130.     }  
  131.   
  132.     /** 
  133.      * [銀行卡號] 前六位,後四位,其他用星號隱藏每位1個星號<例子:6222600**********1234> 
  134.      *  
  135.      * @param cardNum 
  136.      * @return 
  137.      */  
  138.     public static String bankCard(String cardNum) {  
  139.         if (StringUtils.isBlank(cardNum)) {  
  140.             return "";  
  141.         }  
  142.         return StringUtils.left(cardNum, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(cardNum, 4), StringUtils.length(cardNum), "*"), "******"));  
  143.     }  
  144.   
  145.     /** 
  146.      * [公司開戶銀行聯號] 公司開戶銀行聯行號,顯示前兩位,其他用星號隱藏,每位1個星號<例子:12********> 
  147.      *  
  148.      * @param code 
  149.      * @return 
  150.      */  
  151.     public static String cnapsCode(String code) {  
  152.         if (StringUtils.isBlank(code)) {  
  153.             return "";  
  154.         }  
  155.         return StringUtils.rightPad(StringUtils.left(code, 2), StringUtils.length(code), "*");  
  156.     }  
  157.   
  158.     /** 
  159.      * 獲取脫敏json串 <注意:遞歸引用會導致java.lang.StackOverflowError> 
  160.      *  
  161.      * @param javaBean 
  162.      * @return 
  163.      */  
  164.     public static String getJson(Object javaBean) {  
  165.         String json = null;  
  166.         if (null != javaBean) {  
  167.             Class<? extends Object> raw = javaBean.getClass();  
  168.             try {  
  169.                 if (raw.isInterface())  
  170.                     return json;  
  171.                 Gson g = new Gson();  
  172.                 Object clone = g.fromJson(g.toJson(javaBean, javaBean.getClass()), javaBean.getClass());  
  173.                 Set<Integer> referenceCounter = new HashSet<Integer>();  
  174.                 SensitiveInfoUtils.replace(SensitiveInfoUtils.findAllField(raw), clone, referenceCounter);  
  175.                 json = JSON.toJSONString(clone, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);  
  176.                 referenceCounter.clear();  
  177.                 referenceCounter = null;  
  178.             } catch (Throwable e) {  
  179.                 logger.error("SensitiveInfoUtils.getJson() ERROR", e);  
  180.             }  
  181.         }  
  182.         return json;  
  183.     }  
  184.   
  185.     private static Field[] findAllField(Class<?> clazz) {  
  186.         Field[] fileds = clazz.getDeclaredFields();  
  187.         while (null != clazz.getSuperclass() && !Object.class.equals(clazz.getSuperclass())) {  
  188.             fileds = (Field[]) ArrayUtils.addAll(fileds, clazz.getSuperclass().getDeclaredFields());  
  189.             clazz = clazz.getSuperclass();  
  190.         }  
  191.         return fileds;  
  192.     }  
  193.     private static void replace(Field[] fields, Object javaBean, Set<Integer> referenceCounter) throws IllegalArgumentException, IllegalAccessException {  
  194.         if (null != fields && fields.length > 0) {  
  195.             for (Field field : fields) {  
  196.                 field.setAccessible(true);  
  197.                 if (null != field && null != javaBean) {  
  198.                     Object value = field.get(javaBean);  
  199.                     if (null != value) {  
  200.                         Class<?> type = value.getClass();  
  201.                         // 1.處理子屬性,包括集合中的  
  202.                         if (type.isArray()) {  
  203.                             int len = Array.getLength(value);  
  204.                             for (int i = 0; i < len; i++) {  
  205.                                 Object arrayObject = Array.get(value, i);  
  206.                                 SensitiveInfoUtils.replace(SensitiveInfoUtils.findAllField(arrayObject.getClass()), arrayObject, referenceCounter);  
  207.                             }  
  208.                         } else if (value instanceof Collection<?>) {  
  209.                             Collection<?> c = (Collection<?>) value;  
  210.                             Iterator<?> it = c.iterator();  
  211.                             while (it.hasNext()) {  
  212.                                 Object collectionObj = it.next();  
  213.                                 SensitiveInfoUtils.replace(SensitiveInfoUtils.findAllField(collectionObj.getClass()), collectionObj, referenceCounter);  
  214.                             }  
  215.                         } else if (value instanceof Map<?, ?>) {  
  216.                             Map<?, ?> m = (Map<?, ?>) value;  
  217.                             Set<?> set = m.entrySet();  
  218.                             for (Object o : set) {  
  219.                                 Entry<?, ?> entry = (Entry<?, ?>) o;  
  220.                                 Object mapVal = entry.getValue();  
  221.                                 SensitiveInfoUtils.replace(SensitiveInfoUtils.findAllField(mapVal.getClass()), mapVal, referenceCounter);  
  222.                             }  
  223.                         } else if (!type.isPrimitive()  
  224.                                    && !StringUtils.startsWith(type.getPackage().getName(), "javax.")  
  225.                                    && !StringUtils.startsWith(type.getPackage().getName(), "java.")  
  226.                                    && !StringUtils.startsWith(field.getType().getName(), "javax.")  
  227.                                    && !StringUtils.startsWith(field.getName(), "java.")  
  228.                                    && referenceCounter.add(value.hashCode())) {  
  229.                             SensitiveInfoUtils.replace(SensitiveInfoUtils.findAllField(type), value, referenceCounter);  
  230.                         }  
  231.                     }  
  232.                     // 2. 處理自身的屬性  
  233.                     SensitiveInfo annotation = field.getAnnotation(SensitiveInfo.class);  
  234.                     if (field.getType().equals(String.class) && null != annotation) {  
  235.                         String valueStr = (String) value;  
  236.                         if (StringUtils.isNotBlank(valueStr)) {  
  237.                             switch (annotation.type()) {  
  238.                                 case CHINESE_NAME: {  
  239.                                     field.set(javaBean, SensitiveInfoUtils.chineseName(valueStr));  
  240.                                     break;  
  241.                                 }  
  242.                                 case ID_CARD: {  
  243.                                     field.set(javaBean, SensitiveInfoUtils.idCardNum(valueStr));  
  244.                                     break;  
  245.                                 }  
  246.                                 case FIXED_PHONE: {  
  247.                                     field.set(javaBean, SensitiveInfoUtils.fixedPhone(valueStr));  
  248.                                     break;  
  249.                                 }  
  250.                                 case MOBILE_PHONE: {  
  251.                                     field.set(javaBean, SensitiveInfoUtils.mobilePhone(valueStr));  
  252.                                     break;  
  253.                                 }  
  254.                                 case ADDRESS: {  
  255.                                     field.set(javaBean, SensitiveInfoUtils.address(valueStr, 4));  
  256.                                     break;  
  257.                                 }  
  258.                                 case EMAIL: {  
  259.                                     field.set(javaBean, SensitiveInfoUtils.email(valueStr));  
  260.                                     break;  
  261.                                 }  
  262.                                 case BANK_CARD: {  
  263.                                     field.set(javaBean, SensitiveInfoUtils.bankCard(valueStr));  
  264.                                     break;  
  265.                                 }  
  266.                                 case CNAPS_CODE: {  
  267.                                     field.set(javaBean, SensitiveInfoUtils.cnapsCode(valueStr));  
  268.                                     break;  
  269.                                 }  
  270.                             }  
  271.                         }  
  272.                     }  
  273.                 }  
  274.             }  
  275.         }  
  276.     }  
  277.      
  278. //----------------------------------------------------------------------------------------------  
  279.     public static Method [] findAllMethod(Class<?> clazz){  
  280.         Method [] methods= clazz.getMethods();  
  281.         return methods;  
  282.     }  
  283.     
  284.   //----------------------------------------------------------------------------------------------  
  285.     public static enum SensitiveType {  
  286.         /** 
  287.          * 中文名 
  288.          */  
  289.         CHINESE_NAME,  
  290.   
  291.         /** 
  292.          * 身份證號 
  293.          */  
  294.         ID_CARD,  
  295.         /** 
  296.          * 座機號 
  297.          */  
  298.         FIXED_PHONE,  
  299.         /** 
  300.          * 手機號 
  301.          */  
  302.         MOBILE_PHONE,  
  303.         /** 
  304.          * 地址 
  305.          */  
  306.         ADDRESS,  
  307.         /** 
  308.          * 電子郵件 
  309.          */  
  310.         EMAIL,  
  311.         /** 
  312.          * 銀行卡 
  313.          */  
  314.         BANK_CARD,  
  315.         /** 
  316.          * 公司開戶銀行聯號 
  317.          */  
  318.         CNAPS_CODE;  
  319.     }  
  320. }  

聲明註解:

[java] view plain copy
  1. import java.lang.annotation.Documented;  
  2. import java.lang.annotation.ElementType;  
  3. import java.lang.annotation.Inherited;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. import com.ucf.platform.framework.core.util.SensitiveInfoUtils;  
  9.   
  10. /** 
  11.  * @Title: SensitiveInfo.java 
  12.  * @Copyright: Copyright (c) 2015 
  13.  * @Description: <br> 
  14.  *               敏感信息註解標記 <br> 
  15.  */  
  16. @Target({ElementType.FIELD,ElementType.METHOD})  
  17. @Retention(RetentionPolicy.RUNTIME)  
  18. @Inherited  
  19. @Documented  
  20. public @interface SensitiveInfo {  
  21.   
  22.     public SensitiveInfoUtils.SensitiveType type() ;  
  23. }  

測試:

[java] view plain copy
  1. public class JavaBeanA {  
  2.       
  3.     public JavaBeanA(String name,String id){  
  4.           
  5.     }  
  6.       
  7.     @SensitiveInfo(type=SensitiveType.CHINESE_NAME)  
  8.     private String name = "A先生";  
  9.       
  10.     private JavaBeanB b;  
  11.       
  12.     private Date date;  
  13.       
  14.     private List<JavaBeanB> list;  
  15.       
  16.     private Map<String,JavaBeanB> map;  
  17.   
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.   
  26.     public JavaBeanB getB() {  
  27.         return b;  
  28.     }  
  29.   
  30.     public void setB(JavaBeanB b) {  
  31.         this.b = b;  
  32.     }  
  33.   
  34.     public List<JavaBeanB> getList() {  
  35.         return list;  
  36.     }  
  37.   
  38.     public void setList(List<JavaBeanB> list) {  
  39.         this.list = list;  
  40.     }  
  41.   
  42.     public Map<String, JavaBeanB> getMap() {  
  43.         return map;  
  44.     }  
  45.   
  46.     public void setMap(Map<String, JavaBeanB> map) {  
  47.         this.map = map;  
  48.     }  
  49.   
  50.     public Date getDate() {  
  51.         return date;  
  52.     }  
  53.   
  54.     public void setDate(Date date) {  
  55.         this.date = date;  
  56.     }  
  57.       
  58. }  

[java] view plain copy
  1. public class JavaBeanB {  
  2.     @SensitiveInfo(type=SensitiveType.CHINESE_NAME)  
  3.     private String name = "B先生";  
  4.       
  5.     private JavaBeanA a;  
  6.       
  7.     private Set<JavaBeanA> list;  
  8.       
  9.     private Map<String,JavaBeanA> map;  
  10.   
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.   
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.   
  19.     public JavaBeanA getA() {  
  20.         return a;  
  21.     }  
  22.   
  23.     public void setA(JavaBeanA a) {  
  24.         this.a = a;  
  25.     }  
  26.   
  27.     public Set<JavaBeanA> getList() {  
  28.         return list;  
  29.     }  
  30.   
  31.     public void setList(Set<JavaBeanA> list) {  
  32.         this.list = list;  
  33.     }  
  34.   
  35.     public Map<String, JavaBeanA> getMap() {  
  36.         return map;  
  37.     }  
  38.   
  39.     public void setMap(Map<String, JavaBeanA> map) {  
  40.         this.map = map;  
  41.     }  
  42. }  

[java] view plain copy
  1. public class SensitiveInfoUtilsTest {  
  2.       
  3.     /** 
  4.      * [中文姓名] 只顯示第一個漢字,其他隱藏爲2個星號<例子:李**> 
  5.      */  
  6.     @Test  
  7.     public void testChineseNameString() {  
  8.         System.out.println(SensitiveInfoUtils.chineseName("李先生"));  
  9.     }  
  10.   
  11.     /** 
  12.      * [中文姓名] 只顯示第一個漢字,其他隱藏爲2個星號<例子:李**> 
  13.      */  
  14.     @Test  
  15.     public void testChineseNameStringString() {  
  16.         System.out.println(SensitiveInfoUtils.chineseName("李","雷"));  
  17.     }  
  18.   
  19.     /** 
  20.      * [身份證號] 顯示最後四位,其他隱藏。共計18位或者15位。<例子:*************5762> 
  21.      */  
  22.     @Test  
  23.     public void testIdCardNum() {  
  24.         System.out.println(SensitiveInfoUtils.idCardNum("1103541983073188711"));  
  25.     }  
  26.   
  27.     /** 
  28.      * [固定電話] 後四位,其他隱藏<例子:****1234> 
  29.      */  
  30.     @Test  
  31.     public void testFixedPhone() {  
  32.         System.out.println(SensitiveInfoUtils.fixedPhone("01077482277"));  
  33.     }  
  34.   
  35.     /** 
  36.      * [手機號碼] 前三位,後四位,其他隱藏<例子:138******1234> 
  37.      */  
  38.     @Test  
  39.     public void testMobilePhone() {  
  40.         System.out.println(SensitiveInfoUtils.mobilePhone("13777446578"));  
  41.     }  
  42.   
  43.     /** 
  44.      * [地址] 只顯示到地區,不顯示詳細地址;我們要對個人信息增強保護<例子:北京市海淀區****> 
  45.      */  
  46.     @Test  
  47.     public void testAddress() {  
  48.         System.out.println(SensitiveInfoUtils.address("北京朝陽區酒仙橋中路26號院4號樓人人大廈",8));  
  49.     }  
  50.   
  51.     /** 
  52.      * [電子郵箱] 郵箱前綴僅顯示第一個字母,前綴其他隱藏,用星號代替,@及後面的地址顯示<例子:g**@163.com> 
  53.      */  
  54.     @Test  
  55.     public void testEmail() {  
  56.         System.out.println(SensitiveInfoUtils.email("[email protected]"));  
  57.     }  
  58.   
  59.     /** 
  60.      * [銀行卡號] 前六位,後四位,其他用星號隱藏每位1個星號<例子:6222600**********1234> 
  61.      */  
  62.     @Test  
  63.     public void testBankCard() {  
  64.         System.out.println(SensitiveInfoUtils.bankCard("6228480402565890018"));  
  65.     }  
  66.   
  67.     /** 
  68.      *  [公司開戶銀行聯號] 公司開戶銀行聯行號,顯示前兩位,其他用星號隱藏,每位1個星號<例子:12********> 
  69.      */  
  70.     @Test  
  71.     public void testCnapsCode() {  
  72.         System.out.println(SensitiveInfoUtils.cnapsCode("102100029679"));  
  73.     }  
  74.   
  75.     /** 
  76.      * 獲取脫敏json串 <注意:遞歸引用會導致java.lang.StackOverflowError> 
  77.      */  
  78.     @Test  
  79.     public void testGetJson() {  
  80. //        ThreadPoolExecutor consumeExecutor = new ThreadPoolExecutor(30, 30 + 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(30 + 10), new ThreadFactory() {  
  81. //            @Override  
  82. //            public Thread newThread(Runnable r) {  
  83. //                Thread myThread = new Thread(r);  
  84. //                myThread.setName("TT");  
  85. //                return myThread;  
  86. //            }  
  87. //        }, new ThreadPoolExecutor.CallerRunsPolicy());  
  88. //        while (true) {  
  89. //            consumeExecutor.execute(new Runnable() {  
  90. //                @Override  
  91. //                public void run() {}  
  92. //            });  
  93. //        }  
  94.   
  95.         JavaBeanA a1 = new JavaBeanA("","");  
  96.         JavaBeanA a2 = new JavaBeanA("","");  
  97.         JavaBeanB b1 = new JavaBeanB();  
  98.         a1.setB(b1);  
  99. //        a1.setDate(new Date());  
  100.           
  101.         List<JavaBeanB> a1l = new ArrayList<JavaBeanB>();  
  102.         a1l.add(b1);  
  103.         a1.setList(a1l);  
  104.         Map<String, JavaBeanB> a1m = new HashMap<String, JavaBeanB>();  
  105.         a1m.put("b1", b1);  
  106.         a1.setMap(a1m);  
  107.   
  108.         b1.setA(a2);  
  109.         Set<JavaBeanA> b1l = new HashSet<JavaBeanA>();  
  110.         b1.setList(b1l);  
  111.         Map<String, JavaBeanA> b1m = new HashMap<String, JavaBeanA>();  
  112.         b1m.put("a2", a2);  
  113.         b1.setMap(b1m);  
  114.         long t = System.currentTimeMillis();  
  115.         System.out.println(t);  
  116.         System.out.println(SensitiveInfoUtils.getJson(a1));  
  117.         System.out.println(System.currentTimeMillis()-t);  
  118.         System.out.println(JSON.toJSON(a1));  
  119.         System.out.println(System.currentTimeMillis()-t);  
  120.       
  121.     }  
  122.       
  123.   
  124. }  

測試結果:

[html] view plain copy
  1. 李**  
  2. 李*  
  3. ***************8711  
  4. *******2277  
  5. 137****6578  
  6. 北京朝陽區酒仙橋中路26號********  
  7. 6*******@qq.com  
  8. 622848*********0018  
  9. 10**********  
  10. 1443435915750  
  11. {"b":{"a":{"b":null,"date":null,"list":[],"map":null,"name":"A**"},"list":[],"map":{"a2":{"b":null,"date":null,"list":[],"map":null,"name":"A**"}},"name":"B**"},"date":null,"list":[{"a":{"b":null,"date":null,"list":[],"map":null,"name":"A**"},"list":[],"map":{"a2":{"b":null,"date":null,"list":[],"map":null,"name":"A**"}},"name":"B**"}],"map":{"b1":{"a":{"b":null,"date":null,"list":[],"map":null,"name":"A**"},"list":[],"map":{"a2":{"b":null,"date":null,"list":[],"map":null,"name":"A**"}},"name":"B**"}},"name":"A**"}  
  12. 289  
  13. {"b":{"a":{"name":"A先生"},"list":[],"map":{"a2":{"name":"A先生"}},"name":"B先生"},"list":[{"a":{"name":"A先生"},"list":[],"map":{"a2":{"name":"A先生"}},"name":"B先生"}],"map":{"b1":{"a":{"name":"A先生"},"list":[],"map":{"a2":{"name":"A先生"}},"name":"B先生"}},"name":"A先生"}  
  14. 300  



使用了google 的API, 可以使用maven在添加,配置如下:

[html] view plain copy
  1. <!-- gson -->  
  2.         <dependency>  
  3.             <groupId>com.google.code.gson</groupId>  
  4.             <artifactId>gson</artifactId>  
  5.         </dependency>  

.


說明:在需要脫敏的字段上使用定義好的註解,在具體的使用時用SensitiveInfoUtils.getJson(a1),如果不需要脫敏的輸出,儘量不要打印JSON,使用對象的toString()輸出。效率更高。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章