自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一個注解優(yōu)雅的實現(xiàn):接口數(shù)據(jù)脫敏

開發(fā)
數(shù)據(jù)脫敏有很多種實現(xiàn)方式,關(guān)鍵是哪種更加適合,哪種更加優(yōu)雅。下面,我們來看如何優(yōu)雅的實現(xiàn)。

通常接口返回值中的一些敏感數(shù)據(jù)也是要脫敏的,比如身份證號、手機號碼、地址.....通常的手段就是用*隱藏一部分?jǐn)?shù)據(jù),當(dāng)然也可以根據(jù)自己需求定制。

言歸正傳,如何優(yōu)雅的實現(xiàn)呢?有兩種實現(xiàn)方案,如下:

  • 整合Mybatis插件,在查詢的時候針對特定的字段進(jìn)行脫敏
  • 整合Jackson,在序列化階段對特定字段進(jìn)行脫敏
  • 基于Sharding Sphere實現(xiàn)數(shù)據(jù)脫敏

第一種方案網(wǎng)上很多實現(xiàn)方式,下面演示第二種,整合Jackson。

1. 自定義一個Jackson注解

需要自定義一個脫敏注解,一旦有屬性被標(biāo)注,則進(jìn)行對應(yīng)得脫敏,如下:

/**
 * 自定義jackson注解,標(biāo)注在屬性上
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveJsonSerializer.class)
public @interface Sensitive {
    //脫敏策略
    SensitiveStrategy strategy();
}

2. 定制脫敏策略

針對項目需求,定制不同字段的脫敏規(guī)則,比如手機號中間幾位用*替代,如下:

/**
 * 脫敏策略,枚舉類,針對不同的數(shù)據(jù)定制特定的策略
 */
public enum SensitiveStrategy {
    /**
     * 用戶名
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
    /**
     * 身份證
     */
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
    /**
     * 手機號
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * 地址
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****"));


    private final Function<String, String> desensitizer;

    SensitiveStrategy(Function<String, String> desensitizer) {
        this.desensitizer = desensitizer;
    }

    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}

以上只是提供了部分,具體根據(jù)自己項目要求進(jìn)行配置。

3. 定制JSON序列化實現(xiàn)

下面將是重要實現(xiàn),對標(biāo)注注解@Sensitive的字段進(jìn)行脫敏,實現(xiàn)如下:

/**
 * 序列化注解自定義實現(xiàn)
 * JsonSerializer<String>:指定String 類型,serialize()方法用于將修改后的數(shù)據(jù)載入
 */
public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {
    private SensitiveStrategy strategy;

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(strategy.desensitizer().apply(value));
    }

    /**
     * 獲取屬性上的注解屬性
     */
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {

        Sensitive annotation = property.getAnnotation(Sensitive.class);
        if (Objects.nonNull(annotation)&&Objects.equals(String.class, property.getType().getRawClass())) {
            this.strategy = annotation.strategy();
            return this;
        }
        return prov.findValueSerializer(property.getType(), property);

    }
}

4. 定義Person類,對其數(shù)據(jù)脫敏

使用注解@Sensitive注解進(jìn)行數(shù)據(jù)脫敏,代碼如下:

@Data
public class Person {
    /**
     * 真實姓名
     */
    @Sensitive(strategy = SensitiveStrategy.USERNAME)
    private String realName;
    /**
     * 地址
     */
    @Sensitive(strategy = SensitiveStrategy.ADDRESS)
    private String address;
    /**
     * 電話號碼
     */
    @Sensitive(strategy = SensitiveStrategy.PHONE)
    private String phoneNumber;
    /**
     * 身份證號碼
     */
    @Sensitive(strategy = SensitiveStrategy.ID_CARD)
    private String idCard;
}

5. 模擬接口測試

以上4個步驟完成了數(shù)據(jù)脫敏的Jackson注解,下面寫個controller進(jìn)行測試,代碼如下:

@RestController
public class TestController {
    @GetMapping("/test")
    public Person test(){
        Person user = new Person();
        user.setRealName("不才陳某");
        user.setPhoneNumber("19796328206");
        user.setAddress("浙江省杭州市....");
        user.setIdCard("4333333333334334333");
        return user;
    }
}

調(diào)用接口查看數(shù)據(jù)有沒有正常脫敏,結(jié)果如下:

{
    "realName": "不*陳某",
    "address": "浙江省****市..****",
    "phoneNumber": "197****8206",
    "idCard": "4333****34333"
}

6. 總結(jié)

數(shù)據(jù)脫敏有很多種實現(xiàn)方式,關(guān)鍵是哪種更加適合,哪種更加優(yōu)雅.....

責(zé)任編輯:趙寧寧 來源: 碼猿技術(shù)專欄
相關(guān)推薦

2022-06-21 14:44:38

接口數(shù)據(jù)脫敏

2024-11-07 10:55:26

2025-02-23 08:00:00

冪等性Java開發(fā)

2023-08-01 08:54:02

接口冪等網(wǎng)絡(luò)

2022-06-10 13:03:44

接口重試while

2022-05-16 10:45:22

Redis接口限流緩存

2023-06-06 08:51:06

2022-12-12 08:14:47

2022-05-26 10:42:30

數(shù)據(jù)權(quán)限注解

2025-03-11 08:20:58

2023-10-09 07:37:01

2023-08-21 08:01:03

2022-05-31 08:36:41

微服務(wù)網(wǎng)關(guān)鑒權(quán)

2021-01-04 09:12:31

集合變量

2023-12-30 20:04:51

MyBatis框架數(shù)據(jù)

2022-06-20 08:37:28

接口tokenAO

2020-12-08 08:08:51

Java接口數(shù)據(jù)

2023-04-17 08:56:29

微服務(wù)鑒權(quán)業(yè)務(wù)

2020-11-03 16:00:33

API接口微服務(wù)框架編程語言

2024-11-11 11:30:34

點贊
收藏

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