温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中怎么使用sensitive日志脱敏框架

发布时间:2021-06-17 14:12:08 来源:亿速云 阅读:115 作者:Leah 栏目:编程语言

Java中怎么使用sensitive日志脱敏框架,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

项目介绍

日志脱敏是常见的安全需求。普通的基于工具类方法的方式,对代码的入侵性太强。编写起来又特别麻烦。

本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发。

用户也可以基于自己的实际需要,自定义注解。

日志脱敏

为了金融交易的安全性,国家强制规定对于以下信息是要日志脱敏的:

  • 用户名

  • 手机号

  • 邮箱

  • 银行卡号

  • 密码

持久化加密

存储的时候上面的信息都需要加密,密码为不可逆加密,其他为可逆加密。

类似的功能有很多。不在本系统的解决范围内。

特性

  • 基于注解的日志脱敏

  • 可以自定义策略实现,策略生效条件

  • 常见的脱敏内置方案

  • 支持 jdk1.7+

快速开始

maven 导入

<dependency>
  <groupId>com.github.houbb</groupId>
  <artifactId>sensitive-core</artifactId>
  <version>0.0.1</version>
</dependency>

定义对象

User.java

我们对 password 使用脱敏,指定脱敏策略为 StrategyPassword。(直接返回 null)

public class User {

  @Sensitive(strategy = StrategyChineseName.class)
  private String username;
  
  @Sensitive(strategy = StrategyCardId.class)
  private String idCard;
  
  @Sensitive(strategy = StrategyPassword.class)
  private String password;
  
  @Sensitive(strategy = StrategyEmail.class)
  private String email;
  
  @Sensitive(strategy = StrategyPhone.class)
  private String phone;
  
  //Getter & Setter
  //toString()
}

测试

  @Test
  public void UserSensitiveTest() {
    User user = buildUser();
    System.out.println("脱敏前原始: " + user);
    User sensitiveUser = SensitiveUtil.desCopy(user);
    System.out.println("脱敏对象: " + sensitiveUser);
    System.out.println("脱敏后原始: " + user);
  }

  private User buildUser() {
    User user = new User();
    user.setUsername("脱敏君");
    user.setPassword("123456");
    user.setEmail("12345@qq.com");
    user.setIdCard("123456190001011234");
    user.setPhone("18888888888");
    return user;
  }

输出信息如下

脱敏前原始: User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}
脱敏对象: User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}
脱敏后原始: User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}

我们可以直接利用 sensitiveUser 去打印日志信息,而这个对象对于代码其他流程不影响,我们依然可以使用原来的 user 对象。

自定义脱敏策略生效的场景

默认情况下,我们指定的场景都是生效的。

但是你可能需要有些情况下不进行脱敏,比如有些用户密码为 123456,你觉得这种用户不脱敏也罢。

UserPasswordCondition.java

@Sensitive(condition = ConditionFooPassword.class, strategy = StrategyPassword.class)
private String password;

其他保持不变,我们指定了一个 condition,实现如下:

ConditionFooPassword.java

public class ConditionFooPassword implements ICondition {
  @Override
  public boolean valid(IContext context) {
    try {
      Field field = context.getCurrentField();
      final Object currentObj = context.getCurrentObject();
      final String password = (String) field.get(currentObj);
      return !password.equals("123456");
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }
}

也就是只有当密码不是 123456 时密码脱敏策略才会生效。

针对单个字段

上面的例子是基于注解式的编程,如果你只是单个字段。比如

singleSensitiveTest

@Test
public void singleSensitiveTest() {
  final String email = "123456@qq.com";
  IStrategy strategy = new StrategyEmail();
  final String emailSensitive = (String) strategy.des(email, null);
  System.out.println("脱敏后的邮箱:" + emailSensitive);
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI