Java日志脫敏框架 sensitive-v0.0.4 系統(tǒng)內(nèi)置常見(jiàn)注解,支持自定義注解

項(xiàng)目介紹

日志脫敏是常見(jiàn)的安全需求。普通的基于工具類(lèi)方法的方式,對(duì)代碼的入侵性太強(qiáng)。編寫(xiě)起來(lái)又特別麻煩。

本項(xiàng)目提供基于注解的方式,并且內(nèi)置了常見(jiàn)的脫敏方式,便于開(kāi)發(fā)。

特性

  • 基于注解的日志脫敏。
  • 可以自定義策略實(shí)現(xiàn),策略生效條件。
  • 常見(jiàn)的脫敏內(nèi)置方案。
  • java 深拷貝,且原始對(duì)象不用實(shí)現(xiàn)任何接口。
  • 支持用戶(hù)自定義注解。

自定義注解

maven 導(dǎo)入

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>sensitive</artifactId>
    <version>0.0.4</version>
</dependency>

自定義注解

v0.0.4 新增功能。允許功能自定義條件注解和策略注解。
案例

自定義注解

  • 策略脫敏
/**
 * 自定義密碼脫敏策略
 * @author binbin.hou
 * date 2019/1/17
 * @since 0.0.4
 */
@Inherited
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@SensitiveStrategy(CustomPasswordStrategy.class)
public @interface SensitiveCustomPasswordStrategy {
}
  • 脫敏生效條件
/**
 * 自定義密碼脫敏策略生效條件
 * @author binbin.hou
 * date 2019/1/17
 * @since 0.0.4
 */
@Inherited
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@SensitiveCondition(ConditionFooPassword.class)
public @interface SensitiveCustomPasswordCondition{
}
  • TIPS
    @SensitiveStrategy

策略單獨(dú)使用的時(shí)候,默認(rèn)是生效的。

如果有 @SensitiveCondition
注解,則只有當(dāng)條件滿(mǎn)足時(shí),才會(huì)執(zhí)行脫敏策略。

@SensitiveCondition
只會(huì)對(duì)系統(tǒng)內(nèi)置注解和自定義注解生效,因?yàn)?@Sensitive
有屬于自己的策略生效條件。

  • 策略?xún)?yōu)先級(jí)
    @Sensitive

優(yōu)先生效,然后是系統(tǒng)內(nèi)置注解,最后是用戶(hù)自定義注解。

對(duì)應(yīng)的實(shí)現(xiàn)

兩個(gè)元注解 @SensitiveStrategy
@SensitiveCondition
分別指定了對(duì)應(yīng)的實(shí)現(xiàn)。

  • CustomPasswordStrategy.java
public class CustomPasswordStrategy implements IStrategy {

    @Override
    public Object des(Object original, IContext context) {
        return "**********************";
    }

}
  • ConditionFooPassword.java
/**
 * 讓這些 123456 的密碼不進(jìn)行脫敏
 * @author binbin.hou
 * date 2019/1/2
 * @since 0.0.1
 */
public class ConditionFooPassword implements ICondition {
    @Override
    public boolean valid(IContext context) {
        try {
            Field field = context.getCurrentField();            final Object currentObj = context.getCurrentObject();
            final String name = (String) field.get(currentObj);
            return !name.equals("123456");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}

定義測(cè)試對(duì)象

定義一個(gè)使用自定義注解的對(duì)象。

public class CustomPasswordModel {

    @SensitiveCustomPasswordCondition
    @SensitiveCustomPasswordStrategy
    private String password;

    @SensitiveCustomPasswordCondition
    @SensitiveStrategyPassword
    private String fooPassword;
    
    //其他方法
}

測(cè)試

/**
 * 自定義注解測(cè)試
 */
@Test
public void customAnnotationTest() {
    final String originalStr = "CustomPasswordModel{password='hello', fooPassword='123456'}";
    final String sensitiveStr = "CustomPasswordModel{password='**********************', fooPassword='123456'}";
    CustomPasswordModel model = buildCustomPasswordModel();
    Assert.assertEquals(originalStr, model.toString());

    CustomPasswordModel sensitive = SensitiveUtil.desCopy(model);
    Assert.assertEquals(sensitiveStr, sensitive.toString());
    Assert.assertEquals(originalStr, model.toString());
}

構(gòu)建對(duì)象的方法如下:

/**
 * 構(gòu)建自定義密碼對(duì)象
 * @return 對(duì)象
 */
private CustomPasswordModel buildCustomPasswordModel(){
    CustomPasswordModel model = new CustomPasswordModel();
    model.setPassword("hello");
    model.setFooPassword("123456");
    return model;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容