偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

改造了以前寫的數(shù)據(jù)脫敏插件,更好用了

開發(fā) 開發(fā)工具
以前用Mybatis插件的形式寫了一個數(shù)據(jù)脫敏工具,但是發(fā)現(xiàn)有一定的局限性。

[[404361]]

以前用Mybatis插件的形式寫了一個數(shù)據(jù)脫敏工具,但是發(fā)現(xiàn)有一定的局限性。很多時候我們從ORM查詢到的數(shù)據(jù)有其它邏輯要處理,比如根據(jù)電話號查詢用戶信息,你脫敏了就沒有辦法來處理該邏輯了。所以脫敏這個步驟需要后置,放在JSON序列化這個階段比較合適。今天就來實現(xiàn)這個功能。

Jackson序列化中脫敏

改造過程其實就是把脫敏后置到JSON序列化過程中,這里我使用Jackson類庫。

原來Mybatis插件中的脫敏注解是這樣的:

  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target(ElementType.FIELD) 
  3. public @interface Sensitive { 
  4.     SensitiveStrategy strategy(); 

脫敏的策略是這樣的:

  1. import java.util.function.Function
  2.  
  3. /** 
  4.  * 脫敏策略. 
  5.  * 
  6.  * @author felord.cn 
  7.  * @since 11 :25 
  8.  */ 
  9. public enum SensitiveStrategy { 
  10.     /** 
  11.      * Username sensitive strategy. 
  12.      */ 
  13.     USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)""$1*$2")), 
  14.     /** 
  15.      * Id card sensitive type. 
  16.      */ 
  17.     ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})""$1****$2")), 
  18.     /** 
  19.      * Phone sensitive type. 
  20.      */ 
  21.     PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})""$1****$2")), 
  22.     /** 
  23.      * Address sensitive type. 
  24.      */ 
  25.     ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}""$1****$2****")); 
  26.  
  27.  
  28.     private final Function<String, String> desensitizer; 
  29.  
  30.     SensitiveStrategy(Function<String, String> desensitizer) { 
  31.         this.desensitizer = desensitizer; 
  32.     } 
  33.  
  34.     public Function<String, String> desensitizer() { 
  35.         return desensitizer; 
  36.     } 

我將改造它們,是它們在JSON序列化中實現(xiàn)字段屬性脫敏。

自定義脫敏序列化

這里我們首先實現(xiàn)自定義的脫敏序列化邏輯:

  1. import com.fasterxml.jackson.core.JsonGenerator; 
  2. import com.fasterxml.jackson.databind.BeanProperty; 
  3. import com.fasterxml.jackson.databind.JsonMappingException; 
  4. import com.fasterxml.jackson.databind.JsonSerializer; 
  5. import com.fasterxml.jackson.databind.SerializerProvider; 
  6. import com.fasterxml.jackson.databind.ser.ContextualSerializer; 
  7.  
  8. import java.io.IOException; 
  9. import java.util.Objects; 
  10.  
  11. /** 
  12.  * @author felord.cn 
  13.  * @since 1.0.8.RELEASE 
  14.  */ 
  15. public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer { 
  16.     private SensitiveStrategy strategy; 
  17.  
  18.     @Override 
  19.     public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException { 
  20.            gen.writeString(strategy.desensitizer().apply(value)); 
  21.     } 
  22.  
  23.     @Override 
  24.     public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { 
  25.  
  26.             Sensitive annotation = property.getAnnotation(Sensitive.class); 
  27.             if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) { 
  28.                 this.strategy = annotation.strategy(); 
  29.                 return this; 
  30.             } 
  31.             return prov.findValueSerializer(property.getType(), property); 
  32.  
  33.     } 

其中createContextual方法用來獲取實體類上的@Sensitive注解并根據(jù)條件初始化對應的JsonSerializer對象;而顧名思義,serialize方法執(zhí)行脫敏序列化邏輯。

改造脫敏注解

然后就是改造@Sensitive,把上面自定義的JSON序列化和脫敏策略綁定到一起。這里用到了Jackson的捆綁注解@JacksonAnnotationsInside,它的作用是將多個注解組合到一起;另外一個是序列化注解@JsonSerialize,它的作用是聲明使用我上面自定義的序列化方法。

  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target(ElementType.FIELD) 
  3. @JacksonAnnotationsInside 
  4. @JsonSerialize(using = SensitiveJsonSerializer.class) 
  5. public @interface Sensitive { 
  6.     SensitiveStrategy strategy(); 

使用

這就改造完成了,我們來試一試。我們定義一個需要脫敏的實體類并根據(jù)字段標記上對應的脫敏注解:

  1. /** 
  2.  * @author felord.cn 
  3.  * @since 1.0.8.RELEASE 
  4.  */ 
  5. @Data 
  6. public class User { 
  7.  
  8.     /** 
  9.      * 真實姓名 
  10.      */ 
  11.     @Sensitive(strategy = SensitiveStrategy.USERNAME) 
  12.     private String realName; 
  13.     /** 
  14.      * 地址 
  15.      */ 
  16.     @Sensitive(strategy = SensitiveStrategy.ADDRESS) 
  17.     private String address; 
  18.     /** 
  19.      * 電話號碼 
  20.      */ 
  21.     @Sensitive(strategy = SensitiveStrategy.PHONE) 
  22.     private String phoneNumber; 
  23.     /** 
  24.      * 身份證號碼 
  25.      */ 
  26.     @Sensitive(strategy = SensitiveStrategy.ID_CARD) 
  27.     private String idCard; 

然后Jackson來序列化User實例:

  1. User user = new User(); 
  2.  
  3.   user.setRealName("張三豐"); 
  4.   user.setPhoneNumber("13333333333"); 
  5.   user.setAddress("湖北省十堰市丹江口市武當山"); 
  6.   user.setIdCard("4333333333334334333"); 
  7.  
  8.   ObjectMapper objectMapper = new ObjectMapper(); 
  9.  
  10.   String json = objectMapper.writeValueAsString(user); 
  11.  
  12.   System.out.println(json); 

可以得到:

  1.     "realName":"張*豐"
  2.     "address":"湖北省****市丹江口市武****"
  3.     "phoneNumber":"133****3333"
  4.     "idCard":"4333****34333" 

效果還是可以的,當然如果能加個開關(guān)就更好了,根據(jù)不同的場景來決定是否進行脫敏。

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)小胖哥」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)小胖哥公眾號。

 

責任編輯:武曉燕 來源: 碼農(nóng)小胖哥
相關(guān)推薦

2022-02-10 23:37:10

iOS蘋果功能

2023-04-28 12:10:58

ChatGPTAI聯(lián)網(wǎng)模式

2022-04-20 12:17:50

命令Batcat

2022-04-25 10:04:56

df命令Linux

2022-03-11 17:52:23

iOSiPhone

2022-06-02 12:51:14

Windows 11開始菜單

2024-08-13 17:29:24

2021-03-19 09:48:10

Jupyter Not插件Python

2022-02-21 11:31:25

微軟任務欄Windows 11

2018-11-12 00:23:44

谷歌Android開發(fā)者

2022-06-07 10:06:16

Windows 11任務管理器

2010-01-06 13:32:27

JSON數(shù)據(jù)

2021-08-31 09:30:10

Android數(shù)據(jù)庫調(diào)試

2022-06-10 11:32:05

VSCode 中數(shù)據(jù)庫管理SQLTools

2021-07-13 15:35:52

微軟Windows 11Windows

2020-09-08 08:45:39

jupyter插件代碼

2021-03-26 15:18:11

代碼工具Mockoon

2012-12-25 09:58:50

數(shù)據(jù)科學家大數(shù)據(jù)

2020-05-15 15:20:38

指紋識別人臉識別手機

2023-12-12 10:11:41

點贊
收藏

51CTO技術(shù)棧公眾號