国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

解鎖新技能《Java基于注解的脫敏實(shí)現(xiàn)組件SDK》

這篇具有很好參考價(jià)值的文章主要介紹了解鎖新技能《Java基于注解的脫敏實(shí)現(xiàn)組件SDK》。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

平時(shí)開(kāi)發(fā)的過(guò)程中經(jīng)常會(huì)遇到對(duì)一些敏感的字段進(jìn)行脫敏處理,防止信息泄漏,如:郵箱、用戶名、密碼等;做為一個(gè)優(yōu)秀的程序員我們不應(yīng)該遇到這種問(wèn)題時(shí)就做特殊處理,重復(fù)做相同的工作,所以我們應(yīng)該寫(xiě)一個(gè)基礎(chǔ)庫(kù)SDK,解決重復(fù)的問(wèn)題;

開(kāi)源SDK組件
<dependency>
  <groupId>io.github.mingyang66</groupId>
  <artifactId>oceansky-sensitive</artifactId>
  <version>4.3.2</version>
</dependency>

新增JsonNullField注解,可將指定的字段值置為null,注解定義如下:
/**
 * @Description :  自定義注解,標(biāo)注在屬性上,字段屬性值置為null
 * ---------------------------------------------
 * 生效規(guī)則:
 * 1.非int、double、float、byte、short、long、boolean、char八種基本數(shù)據(jù)類型字段才會(huì)生效;
 * 2.
 * ---------------------------------------------
 * @Author :  Emily
 * @CreateDate :  Created in 2023/7/14 5:22 下午
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonNullField {

}

一、定義注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonSensitive {
}

@JsonSensitive標(biāo)注在類上,表示此類需要進(jìn)行脫敏處理;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonSimField {
    /**
     * 脫敏類型,見(jiàn)枚舉類型{@link SensitiveType}
     *
     * @return
     */
    SensitiveType value() default SensitiveType.DEFAULT;
}

@JsonSimField標(biāo)注在類的String、Collection、String[]字段上,表示對(duì)這些字段值進(jìn)行脫敏處理;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonFlexField {
    /**
     * 要隱藏的參數(shù)key名稱
     *
     * @return
     */
    String[] fieldKeys() default {};

    /**
     * 要隱藏的參數(shù)值的key名稱
     *
     * @return
     */
    String fieldValue();

    /**
     * 脫敏類型,見(jiàn)枚舉類型{@link SensitiveType}
     *
     * @return
     */
    SensitiveType[] types() default {};
}

@JsonFlexField注解標(biāo)注在復(fù)雜數(shù)據(jù)類型字段上,具體的使用方法會(huì)在后面舉例說(shuō)明;

二、實(shí)現(xiàn)對(duì)字段脫敏處理的核心實(shí)現(xiàn)類
public class DeSensitiveUtils {

    public static final Logger logger = LoggerFactory.getLogger(DeSensitiveUtils.class);

    /**
     * @param entity 實(shí)體類|普通對(duì)象
     * @return 對(duì)實(shí)體類進(jìn)行脫敏,返回原來(lái)的實(shí)體類對(duì)象
     */
    public static <T> T acquire(final T entity) {
        try {
            if (JavaBeanUtils.isFinal(entity)) {
                return entity;
            }
            if (entity instanceof Collection) {
                for (Iterator it = ((Collection) entity).iterator(); it.hasNext(); ) {
                    acquire(it.next());
                }
            } else if (entity instanceof Map) {
                for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) entity).entrySet()) {
                    acquire(entry.getValue());
                }
            } else if (entity.getClass().isArray()) {
                if (!entity.getClass().getComponentType().isPrimitive()) {
                    for (Object v : (Object[]) entity) {
                        acquire(v);
                    }
                }
            } else if (entity instanceof BaseResponse) {
                acquire(((BaseResponse) entity).getData());
            } else if (entity.getClass().isAnnotationPresent(JsonSensitive.class)) {
                doSetField(entity);
            }
        } catch (IllegalAccessException exception) {
            logger.error(PrintExceptionInfo.printErrorInfo(exception));
        }
        return entity;
    }

    /**
     * @param entity 實(shí)體類對(duì)象
     * @throws IllegalAccessException 非法訪問(wèn)異常
     * @Description 對(duì)實(shí)體類entity的屬性及父類的屬性遍歷并對(duì)符合條件的屬性進(jìn)行多語(yǔ)言翻譯
     */
    protected static <T> void doSetField(final T entity) throws IllegalAccessException {
        Field[] fields = FieldUtils.getAllFields(entity.getClass());
        for (Field field : fields) {
            if (JavaBeanUtils.isModifierFinal(field)) {
                continue;
            }
            field.setAccessible(true);
            Object value = field.get(entity);
            if (Objects.isNull(value)) {
                continue;
            }
            if (value instanceof String) {
                doGetEntityStr(field, entity, value);
            } else if (value instanceof Collection) {
                doGetEntityColl(field, entity, value);
            } else if (value instanceof Map) {
                doGetEntityMap(field, entity, value);
            } else if (value.getClass().isArray()) {
                doGetEntityArray(field, entity, value);
            } else {
                acquire(value);
            }
        }
        doGetEntityFlex(entity);
    }

    /**
     * @param field  實(shí)體類屬性對(duì)象
     * @param entity 實(shí)體類對(duì)象
     * @param value  屬性值對(duì)象
     * @throws IllegalAccessException 拋出非法訪問(wèn)異常
     * @Description 對(duì)字符串進(jìn)行多語(yǔ)言支持
     */
    protected static <T> void doGetEntityStr(final Field field, final T entity, final Object value) throws IllegalAccessException {
        if (field.isAnnotationPresent(JsonSimField.class)) {
            field.set(entity, DataMaskUtils.doGetProperty((String) value, field.getAnnotation(JsonSimField.class).value()));
        } else {
            acquire(value);
        }
    }

    /**
     * @param field  實(shí)體類屬性對(duì)象
     * @param entity 實(shí)體類對(duì)象
     * @param value  屬性值對(duì)象
     * @throws IllegalAccessException 拋出非法訪問(wèn)異常
     * @Description 對(duì)Collection集合中存儲(chǔ)是字符串、實(shí)體對(duì)象進(jìn)行多語(yǔ)言支持
     */
    protected static <T> void doGetEntityColl(final Field field, final T entity, final Object value) throws IllegalAccessException {
        Collection<Object> list = null;
        Collection collection = ((Collection) value);
        for (Iterator it = collection.iterator(); it.hasNext(); ) {
            Object v = it.next();
            if (Objects.isNull(v)) {
                continue;
            }
            if ((v instanceof String) && field.isAnnotationPresent(JsonSimField.class)) {
                list = (list == null) ? Lists.newArrayList() : list;
                list.add(DataMaskUtils.doGetProperty((String) v, field.getAnnotation(JsonSimField.class).value()));
            } else {
                acquire(v);
            }
        }
        if (Objects.nonNull(list)) {
            field.set(entity, list);
        }
    }

    /**
     * @param field  實(shí)體類屬性對(duì)象
     * @param entity 實(shí)體類對(duì)象
     * @param value  屬性值對(duì)象
     * @throws IllegalAccessException 拋出非法訪問(wèn)異常
     * @Description 對(duì)Map集合中存儲(chǔ)是字符串、實(shí)體對(duì)象進(jìn)行多語(yǔ)言支持
     */
    protected static <T> void doGetEntityMap(final Field field, final T entity, final Object value) throws IllegalAccessException {
        Map<Object, Object> dMap = ((Map<Object, Object>) value);
        for (Map.Entry<Object, Object> entry : dMap.entrySet()) {
            Object key = entry.getKey();
            Object v = entry.getValue();
            if (Objects.isNull(v)) {
                continue;
            }
            if ((v instanceof String) && field.isAnnotationPresent(JsonSimField.class)) {
                dMap.put(key, DataMaskUtils.doGetProperty((String) v, field.getAnnotation(JsonSimField.class).value()));
            } else {
                acquire(value);
            }
        }
    }

    /**
     * @param field  實(shí)體類屬性對(duì)象
     * @param entity 實(shí)體類對(duì)象
     * @param value  屬性值對(duì)象
     * @throws IllegalAccessException 拋出非法訪問(wèn)異常
     * @Description 對(duì)數(shù)組中存儲(chǔ)是字符串、實(shí)體對(duì)象進(jìn)行多語(yǔ)言支持
     */
    protected static <T> void doGetEntityArray(final Field field, final T entity, final Object value) throws IllegalAccessException {
        if (value.getClass().getComponentType().isPrimitive()) {
            return;
        }
        Object[] arrays = ((Object[]) value);
        for (int i = 0; i < arrays.length; i++) {
            Object v = arrays[i];
            if (Objects.isNull(v)) {
                continue;
            }
            if ((v instanceof String) && field.isAnnotationPresent(JsonSimField.class)) {
                arrays[i] = DataMaskUtils.doGetProperty((String) v, field.getAnnotation(JsonSimField.class).value());
            } else {
                acquire(value);
            }
        }
    }

    /**
     * @param entity 實(shí)體類對(duì)象
     * @throws IllegalAccessException 拋出非法訪問(wèn)異常
     */
    protected static <T> void doGetEntityFlex(final T entity) throws IllegalAccessException {
        Field[] fields = FieldUtils.getFieldsWithAnnotation(entity.getClass(), JsonFlexField.class);
        for (Field field : fields) {
            field.setAccessible(true);
            Object value = field.get(entity);
            if (Objects.isNull(value)) {
                continue;
            }
            JsonFlexField jsonFlexField = field.getAnnotation(JsonFlexField.class);
            if (Objects.isNull(jsonFlexField.fieldValue())) {
                return;
            }
            Field flexField = FieldUtils.getField(entity.getClass(), jsonFlexField.fieldValue(), true);
            if (Objects.isNull(flexField)) {
                return;
            }
            Object flexValue = flexField.get(entity);
            if (Objects.isNull(flexValue) || !(flexValue instanceof String)) {
                return;
            }
            int index = Arrays.asList(jsonFlexField.fieldKeys()).indexOf((String) value);
            if (index < 0) {
                return;
            }
            SensitiveType type;
            if (index >= jsonFlexField.types().length) {
                type = SensitiveType.DEFAULT;
            } else {
                type = jsonFlexField.types()[index];
            }
            flexField.set(entity, DataMaskUtils.doGetProperty((String) flexValue, type));
        }
    }
}

三、基于注解的脫敏SDK使用案例
  • 對(duì)實(shí)體類中字段為字符串類型脫敏處理
@JsonSensitive
public class PubRequest {
    @JsonSimField(SensitiveType.USERNAME)
    public String username;
    @JsonSimField
    public String password;
    }
  • 對(duì)實(shí)體類中字段是List、Map<String,String>、String[]集合類型進(jìn)行脫敏處理
@JsonSensitive
public class PubRequest {
    @JsonSimField
    public Map<String, String> work;
    @JsonSimField
    public List<String> jobList;
    @JsonSimField
    public String[] jobs;
}
  • 實(shí)體類中的字段是復(fù)雜數(shù)據(jù)類型脫敏處理
@JsonSensitive
public class JsonRequest extends Animal{
    @JsonFlexField(fieldKeys = {"email", "phone"}, fieldValue = "fieldValue", types = {SensitiveType.EMAIL, SensitiveType.PHONE})
    private String fieldKey;
    private String fieldValue;
}

復(fù)雜數(shù)據(jù)類型其實(shí)就是fieldKey可以指定多個(gè)不同的字段名,fieldValue是具體的字段值,如果fieldKey是email時(shí)fieldValue傳遞的就是郵箱,就按照types中指定脫敏策略為郵箱的策略脫敏;

  • 實(shí)體類中的屬性字段是集合類型,集合中存放的是嵌套的實(shí)體類
    @JsonSensitive
    public static class Job {
        @JsonSimField(SensitiveType.DEFAULT)
        private String work;
        @JsonSimField(SensitiveType.EMAIL)
        private String email;
    }

嵌套實(shí)體類屬性字段

    public Job job;
    public Map<String, Object> work;
    public List<PubResponse.Job> jobList;
    public PubResponse.Job[] jobs;

如果實(shí)體類中的集合中存放的是實(shí)體類,并且這個(gè)實(shí)體類標(biāo)注了@JsonSensitive注解,則會(huì)對(duì)嵌套實(shí)體類中標(biāo)注了@JsonSimField、@JsonFlexField注解的字段進(jìn)行脫敏處理;同樣如果最外層是集合、數(shù)組、key-value類型則也會(huì)對(duì)內(nèi)部嵌套的實(shí)體類進(jìn)行脫敏處理;

本文只對(duì)脫敏SDK做大概的闡述,如果你需要源碼可以到個(gè)人GitHub上去拉;本文的示例是對(duì)當(dāng)前實(shí)體類對(duì)象本身進(jìn)行脫敏處理,返回的還是原來(lái)的對(duì)象本身,個(gè)人GitHub示例中還有一個(gè)返回是非當(dāng)前對(duì)象的SDK工具類SensitiveUtils;

GitHub地址:https://github.com/mingyang66/spring-parent文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-423188.html

到了這里,關(guān)于解鎖新技能《Java基于注解的脫敏實(shí)現(xiàn)組件SDK》的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 解鎖新技能《logback標(biāo)記日志過(guò)濾器MarkerFilter》

    開(kāi)源日志SDK(純java版) 在logback-classic中存在一個(gè)全局過(guò)濾器TurboFilter,TurboFilter是與LoggerContext綁定,會(huì)在會(huì)在其它過(guò)濾器之前執(zhí)行;MarkerFilter是TurboFilter的一個(gè)子類,其作用是標(biāo)記日志是否記錄入文件之中,可以指定標(biāo)記的日志記錄到文件中;也可以指定標(biāo)記的日志拒絕記錄到

    2024年02月15日
    瀏覽(14)
  • 解鎖新技能RestTemplate設(shè)置全局、單個(gè)請(qǐng)求超時(shí)時(shí)間及支持https請(qǐng)求

    springboot請(qǐng)求第三方接口時(shí)會(huì)用到RestTemplate,其底層實(shí)現(xiàn)邏輯默認(rèn)是通過(guò)SimpleClientHttpRequestFactory來(lái)實(shí)現(xiàn),具體由socket連接來(lái)實(shí)現(xiàn);可以替換其默認(rèn)實(shí)現(xiàn)為HttpComponentsClientHttpRequestFactory。 一、自定義RestTemplate實(shí)例對(duì)象 二、RestTemplate自定義全局超時(shí)時(shí)間 三、RestTemplate設(shè)置單個(gè)請(qǐng)求

    2023年04月09日
    瀏覽(18)
  • Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法

    在Java中,可以使用各種技術(shù)來(lái)實(shí)現(xiàn)數(shù)據(jù)脫敏,下面將介紹幾種常見(jiàn)的Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法。 字符串截取 字符串截取是一種簡(jiǎn)單的數(shù)據(jù)脫敏方法,它將敏感數(shù)據(jù)的一部分字符替換成“”號(hào)或其他字符。例如,將身份證號(hào)碼的前6位和后4位替換成“”號(hào),這樣可以保護(hù)身份證

    2024年02月16日
    瀏覽(22)
  • Java 實(shí)現(xiàn)數(shù)據(jù)脫敏的詳細(xì)講解

    數(shù)據(jù)脫敏是一種數(shù)據(jù)保護(hù)技術(shù),它通過(guò)對(duì)敏感數(shù)據(jù)進(jìn)行修改或替換,使得數(shù)據(jù)無(wú)法被識(shí)別或關(guān)聯(lián)到個(gè)人身份,從而保護(hù)個(gè)人隱私。在Java中,可以通過(guò)各種技術(shù)來(lái)實(shí)現(xiàn)數(shù)據(jù)脫敏,本文將詳細(xì)講解Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法和技術(shù)。 數(shù)據(jù)脫敏是一種保護(hù)個(gè)人隱私的技術(shù),它通過(guò)對(duì)敏

    2024年02月07日
    瀏覽(15)
  • Java 實(shí)現(xiàn)數(shù)據(jù)脫敏的技術(shù)方案

    數(shù)據(jù)脫敏是保護(hù)個(gè)人隱私的一種重要手段,它通過(guò)對(duì)敏感信息進(jìn)行處理,將敏感信息轉(zhuǎn)換為不敏感的信息,以保護(hù)個(gè)人隱私不被泄漏。在Java中,數(shù)據(jù)脫敏也是一項(xiàng)非常重要的技術(shù),本文將從數(shù)據(jù)脫敏的概念、Java中的數(shù)據(jù)脫敏原理、Java中的數(shù)據(jù)脫敏方法以及如何實(shí)現(xiàn)數(shù)據(jù)脫敏

    2024年02月08日
    瀏覽(36)
  • 8.4Java EE——基于注解的AOP實(shí)現(xiàn)

    8.4Java EE——基于注解的AOP實(shí)現(xiàn)

    元素 描述 @Aspect 配置切面 @Pointcut 配置切點(diǎn) @Before 配置前置通知 @After 配置后置通知 @Around 配置環(huán)繞方式 @AfterReturning 配置返回通知 @AfterThrowing 配置異常通知 下面通過(guò)一個(gè)案例演示基于注解的AOP的實(shí)現(xiàn),案例具體實(shí)現(xiàn)步驟如下。

    2024年02月15日
    瀏覽(20)
  • 解鎖新技能《spring如何將屬性配置文件中的屬性綁定到實(shí)體類中》

    在springboot中將配置文件中的屬性綁定到指定的實(shí)體類上可以通過(guò)自動(dòng)化配置的方式實(shí)現(xiàn),也可以通過(guò)手動(dòng)方式從Environment環(huán)境變量中取出再賦值給實(shí)體類;但是在有些場(chǎng)景下自動(dòng)化配置這種方案是行不通的,例如:ApplicationListener、ApplicationContextInitializer進(jìn)行初始化調(diào)用時(shí)屬性

    2024年02月16日
    瀏覽(56)
  • Java實(shí)現(xiàn)對(duì)手機(jī)號(hào)、身份證號(hào)、護(hù)照號(hào)脫敏

    背景: 我們?cè)陧?xiàng)目中經(jīng)常會(huì)需要用到用戶的敏感信息,比如手機(jī)號(hào)、身份證號(hào)、護(hù)照號(hào); 當(dāng)數(shù)據(jù)需要在頁(yè)面上進(jìn)行展示的時(shí)候就需要進(jìn)行脫敏,將其中幾位變?yōu)?*。 官方文檔: https://www.hutool.cn/docs/#/core/工具類/信息脫敏工具-DesensitizedUtil Hutool依賴: 代碼實(shí)現(xiàn): 執(zhí)行結(jié)果:

    2024年02月15日
    瀏覽(22)
  • 解鎖新技能《Git本地訪問(wèn)GitHub出現(xiàn)WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!警告》

    今天本地git訪問(wèn)github倉(cāng)庫(kù)的時(shí)候出現(xiàn)如下異常: 問(wèn)題原因是SSH會(huì)把每個(gè)曾經(jīng)訪問(wèn)過(guò)的Git服務(wù)器的公鑰記錄在/Users/xx/.ssh/known_hosts文件中,當(dāng)下次訪問(wèn)時(shí)會(huì)核對(duì)公鑰,如果和上次的記錄不同,SSH就會(huì)發(fā)出警告。 解決方法:直接刪除/Users/xx/.ssh/known_hosts文件。 GitHub地址:https://

    2024年02月14日
    瀏覽(26)
  • JAVA學(xué)習(xí)-注解.基于注解的單元測(cè)試

    ? ? ? ? 基于注解的單元測(cè)試是一種使用注解來(lái)簡(jiǎn)化和增強(qiáng)測(cè)試代碼編寫(xiě)和執(zhí)行的方法。在Java中,有多個(gè)基于注解的單元測(cè)試框架可供選擇,包括JUnit、TestNG等。下面將對(duì)幾個(gè)常見(jiàn)的基于注解的單元測(cè)試框架進(jìn)行概述,并介紹它們的特點(diǎn)、使用方法以及與其他框架的比較。

    2024年04月28日
    瀏覽(20)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包