脚本宝典收集整理的这篇文章主要介绍了java 日志脱敏框架 sensitive-v0.0.4 系统内置常见注解,支持自定义注解,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
项目介绍
日志脱敏是常见的安全需求。普通的基于工具类方法的方式,对代码的入侵性太强。编写起来又特别麻烦。
本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发。
特性
自定义注解
maven 导入
<dependency> <groupId>com.github.houbb</groupId> <artifactId>sensitive-core</artifactId> <version>0.0.4</version> </dependency>
自定义注解
v0.0.4 新增功能。允许功能自定义条件注解和策略注解。
案例
@H_777_82@自定义注解 - 策略脱敏
/** * 自定义密码脱敏策略 * @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
策略单独使用的时候,默认是生效的。
如果有 @SensitiveCondition
注解,则只有当条件满足时,才会执行脱敏策略。
@SensitiveCondition
只会对系统内置注解和自定义注解生效,因为 @Sensitive
有属于自己的策略生效条件。
- 策略优先级
@Sensitive
优先生效,然后是系统内置注解,最后是用户自定义注解。
对应的实现
两个元注解 @SensitiveStrategy
、@SensitiveCondition
分别指定了对应的实现。
- CustomPasswordStrategy.java
public class CustomPasswordStrategy implements IStrategy { @override public Object des(Object original, IContext context) { return "**********************"; } }
- ConditionFooPassword.java
/** * 让这些 123456 的密码不进行脱敏 * @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); } } }
定义测试对象
定义一个使用自定义注解的对象。
public class CustomPasswordModel { @SensitiveCustomPasswordCondition @SensitiveCustomPasswordStrategy PRivate String password; @SensitiveCustomPasswordCondition @SensitiveStrategyPassword private String fooPassword; //其他方法 }
测试
/** * 自定义注解测试 */ @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()); }
构建对象的方法如下:
/** * 构建自定义密码对象 * @return 对象 */ private CustomPasswordModel buildCustomPasswordModel(){ CustomPasswordModel model = new CustomPasswordModel(); model.setPassword("hello"); model.setFooPassword("123456"); return model; }
以上是脚本宝典为你收集整理的java 日志脱敏框架 sensitive-v0.0.4 系统内置常见注解,支持自定义注解全部内容,希望文章能够帮你解决java 日志脱敏框架 sensitive-v0.0.4 系统内置常见注解,支持自定义注解所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。