SpringBoot敏感数据脱敏怎么处理

在Spring Boot中处理敏感数据脱敏,可以通过以下几种方式实现,确保敏感信息在接口返回、日志输出、数据库存储等环节得到保护:


1. 使用注解 + Jackson序列化脱敏

通过自定义注解和Jackson的JsonSerializer实现数据脱敏,适合接口返回敏感数据时动态处理。

实现步骤:
  1. 定义脱敏策略枚举
java 复制代码
public enum SensitiveStrategy {
    // 不同脱敏策略
    USERNAME(s -> s.replaceAll("(.).", "$1**")),
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"));

    private final Function<String, String> desensitizer;

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

    public Function<String, String> getDesensitizer() {
        return desensitizer;
    }
}
  1. 自定义脱敏注解
java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveSerialize.class)
public @interface Sensitive {
    SensitiveStrategy strategy();
}
  1. 自定义序列化器
java 复制代码
public class SensitiveSerialize extends JsonSerializer<String> {
    private SensitiveStrategy strategy;

    public SensitiveSerialize(SensitiveStrategy strategy) {
        this.strategy = strategy;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeString(strategy.getDesensitizer().apply(value));
    }
}
  1. 在DTO字段上使用注解
java 复制代码
public class UserDTO {
    @Sensitive(strategy = SensitiveStrategy.PHONE)
    private String phone;

    @Sensitive(strategy = SensitiveStrategy.ID_CARD)
    private String idCard;
}

2. 日志脱敏处理

使用日志框架(如Logback或Log4j2)的替换规则,避免敏感信息写入日志。

Logback配置示例(通过正则替换):
xml 复制代码
<configuration>
    <conversionRule conversionWord="msg" converterClass="com.example.LogMaskConverter"/>
</configuration>

自定义转换器:

java 复制代码
public class LogMaskConverter extends ClassicConverter {
    @Override
    public String convert(ILoggingEvent event) {
        return event.getMessage()
            .replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2") // 手机号
            .replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"); // 身份证
    }
}

3. 数据库加密存储

使用加密工具(如Jasypt)对敏感字段进行加密存储。

实现步骤:
  1. 添加依赖
xml 复制代码
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
  1. 配置加密密钥
properties 复制代码
jasypt.encryptor.password=your_secret_key
  1. 在实体类中使用加密注解
java 复制代码
public class User {
    @Encrypted
    private String phone;

    @Encrypted
    private String idCard;
}

4. 其他注意事项

  • 全局性处理:结合AOP对所有Controller返回结果进行统一脱敏。
  • 深度脱敏:处理嵌套对象(如集合、Map中的敏感数据)。
  • 性能优化:避免在高频接口中使用复杂正则匹配。
  • 测试验证:确保脱敏后的数据不可逆且符合业务需求。

总结

根据场景选择合适方案:

  • 接口脱敏:使用Jackson自定义序列化。
  • 日志安全:配置日志替换规则。
  • 存储安全:数据库字段加密。
  • 传输安全:启用HTTPS + 数据加密传输。

通过组合以上方法,可系统性地保护敏感数据,满足GDPR等数据安全法规要求。

相关推荐
CoderYanger5 分钟前
A.每日一题:输入单词需要的最少按键次数 Ⅰ+Ⅱ
java·数据结构·算法·leetcode·面试
Cosolar8 分钟前
一文弄懂 Agent Harness 与 Agent Runtime 的区别
java·后端·github
吴声子夜歌20 分钟前
Java——类、对象及方法(一)
java·开发语言
程序员夏洛27 分钟前
Redis 的持久化机制有哪些?
java·数据库·redis
凯尔萨厮34 分钟前
Java学习笔记十(注解)
java·笔记·学习
钱栈up1 小时前
VSCode 调试多模块 Maven + Spring Boot 后端:从启动失败到一次跑通
java·spring boot·maven
uoKent1 小时前
c++中new和malloc的区别
java·jvm·c++
十五喵源码网1 小时前
基于springboot2+vue2的疾病防控综合系统
java·毕业设计·springboot·论文笔记
砚底藏山河1 小时前
【量化纯GET实战 #11】移动均线 MA:5 日 20 日 60 日怎么算怎么用
java·python·金融·maven
一只小闪闪1 小时前
easy-trans java数据通用翻译框架v2.3.0
java·后端·架构