网易首页 > 网易号 > 正文 申请入驻

3种常见的数据脱敏方案

0
分享至

点击关注公众号,实用技术文章及时了解


目录

  1. SQL数据脱敏实现

  2. JAVA数据脱敏实现

  3. mybatis-mate-sensitive-jackson

1.SQL数据脱敏实现

MYSQL(电话号码,身份证)数据脱敏的实现

-- CONCAT()、LEFT()和RIGHT()字符串函数组合使用,请看下面具体实现
 
-- CONCAT(str1,str2,…):返回结果为连接参数产生的字符串
-- LEFT(str,len):返回从字符串str 开始的len 最左字符
-- RIGHT(str,len):从字符串str 开始,返回最右len 字符
 
-- 电话号码脱敏sql:
 
SELECT mobilePhone AS 脱敏前电话号码,CONCAT(LEFT(mobilePhone,3), '********' ) AS 脱敏后电话号码 FROM t_s_user
 
-- 身份证号码脱敏sql:
 
SELECT idcard AS 未脱敏身份证, CONCAT(LEFT(idcard,3), '****' ,RIGHT(idcard,4)) AS 脱敏后身份证号 FROM t_s_user
2.JAVA数据脱敏实现

可参考:海强 / sensitive-plus

https://gitee.com/strong_sea/sensitive-plus

数据脱敏插件,目前支持地址脱敏、银行卡号脱敏、中文姓名脱敏、固话脱敏、身份证号脱敏、手机号脱敏、密码脱敏 一个是正则脱敏、另外一个根据显示长度脱敏,默认是正则脱敏,可以根据自己的需要配置自己的规则。

3.mybatis-mate-sensitive-jackson

mybatisplus 的新作,可以测试使用,生产需要收费。

根据定义的策略类型,对数据进行脱敏,当然策略可以自定义。

# 目前已有
package mybatis.mate.strategy;
 
public interface SensitiveType {
    String chineseName = "chineseName";
    String idCard = "idCard";
    String phone = "phone";
    String mobile = "mobile";
    String address = "address";
    String email = "email";
    String bankCard = "bankCard";
    String password = "password";
    String carNumber = "carNumber";
}

Demo 代码目录

1、pom.xml

             
     
                
         
                  
 com.baomidou groupId>         
                    
 mybatis-mate-examples artifactId>         
                      
 0.0.1-SNAPSHOT version>      parent>     
                        
 4.0.0 modelVersion>     
                          
 mybatis-mate-sensitive-jackson artifactId>     
                            
         
                              
             
                                
 mysql groupId>             
                                  
 mysql-connector-java artifactId>          dependency>      dependencies>   project>
                 
                
               
              
             
            
           
          
         
        
      

2、appliation.yml

# DataSource Config
spring:
  datasource:
#    driver-class-name: org.h2.Driver
#    schema: classpath:db/schema-h2.sql
#    data: classpath:db/data-h2.sql
#    url: jdbc:h2:mem:test
#    username: root
#    password: test
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_mate?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
# Mybatis Mate 配置
mybatis-mate:
  cert:
    # 请添加微信wx153666购买授权,不白嫖从我做起! 测试证书会失效,请勿正式环境使用
    grant: thisIsTestLicense
    license: as/bsBaSVrsA9FfjC/N77ruEt2/QZDrW+MHETNuEuZBra5mlaXZU+DE1ZvF8UjzlLCpH3TFVH3WPV+Ya7Ugiz1Rx4wSh/FK6Ug9lhos7rnsNaRB/+mR30aXqtlLt4dAmLAOCT56r9mikW+t1DDJY8TVhERWMjEipbqGO9oe1fqYCegCEX8tVCpToKr5J1g1V86mNsNnEGXujnLlEw9jBTrGxAyQroD7Ns1Dhwz1K4Y188mvmRQp9t7OYrpgsC7N9CXq1s1c2GtvfItHArkqHE4oDrhaPjpbMjFWLI5/XqZDtW3D+AVcH7pTcYZn6vzFfDZEmfDFV5fQlT3Rc+GENEg==
 
# Logger Config
logging:
  level:
    mybatis.mate: debug

3、Appliation启动类

package mybatis.mate.sensitive.jackson;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SensitiveJacksonApplication {
 
    // 测试访问 http://localhost:8080/info ,http://localhost:8080/list
    public static void main(String[] args) {
        SpringApplication.run(SensitiveJacksonApplication.class, args);
    }
}

4、配置类,自定义脱敏策略

package mybatis.mate.sensitive.jackson.config;
 
import mybatis.mate.databind.ISensitiveStrategy;
import mybatis.mate.strategy.SensitiveStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class SensitiveStrategyConfig {
 
    /**      * 注入脱敏策略      */
    @Bean
    public ISensitiveStrategy sensitiveStrategy() {
        // 自定义 testStrategy 类型脱敏处理
        return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
    }
}

5、业务类

User,注解标识脱敏字段,及选用脱敏策略

package mybatis.mate.sensitive.jackson.entity;
 
import lombok.Getter;
import lombok.Setter;
import mybatis.mate.annotation.FieldSensitive;
import mybatis.mate.sensitive.jackson.config.SensitiveStrategyConfig;
import mybatis.mate.strategy.SensitiveType;
 
@Getter
@Setter
public class User {
    private Long id;
    /**      * 这里是一个自定义的策略 {@link SensitiveStrategyConfig} 初始化注入      */
    @FieldSensitive("testStrategy")
    private String username;
    /**      * 默认支持策略 {@link SensitiveType }      */
    @FieldSensitive(SensitiveType.mobile)
    private String mobile;
    @FieldSensitive(SensitiveType.email)
    private String email;
 
}

UserController

package mybatis.mate.sensitive.jackson.controller;
 
import mybatis.mate.databind.ISensitiveStrategy;
import mybatis.mate.databind.RequestDataTransfer;
import mybatis.mate.sensitive.jackson.entity.User;
import mybatis.mate.sensitive.jackson.mapper.UserMapper;
import mybatis.mate.strategy.SensitiveType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ISensitiveStrategy sensitiveStrategy;
 
    // 测试访问 http://localhost:8080/info
    @GetMapping("/info")
    public User info() {
        return userMapper.selectById(1L);
    }
 
    // 测试返回 map 访问 http://localhost:8080/map
    @GetMapping("/map")
    public Map   map ()  {
        // 测试嵌套对象脱敏
        Map
      
  userMap =  new HashMap<>();         userMap.put( "user", userMapper.selectById( 1L));         userMap.put( "test",  123);         userMap.put( "userMap",  new HashMap () {{             put("user2", userMapper.selectById(2L));             put("test2", "hi china");         }});          // 手动调用策略脱敏         userMap.put( "mobile", sensitiveStrategy.getStrategyFunctionMap()                 .get(SensitiveType.mobile).apply( "15315388888"));          return userMap;     }        // 测试访问 http://localhost:8080/list      // 不脱敏 http://localhost:8080/list?skip=1      @GetMapping( "/list")      public List   list (HttpServletRequest request)  {          if ( "1".equals(request.getParameter( "skip"))) {              // 跳过脱密处理             RequestDataTransfer.skipSensitive();         }          return userMapper.selectList( null);     } }

UserMapper

package mybatis.mate.sensitive.jackson.mapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import mybatis.mate.sensitive.jackson.entity.User;
import org.apache.ibatis.annotations.Mapper;
 
@Mapper
public interface UserMapper extends BaseMapper
             
  {   }
      

6、测试

GET http://localhost:8080/list

[
  {
    "id": 1,
    "username": "Jone***test***",
    "mobile": "153******81",
    "email": "t****@baomidou.com"
  },
  {
    "id": 2,
    "username": "Jack***test***",
    "mobile": "153******82",
    "email": "t****@baomidou.com"
  },
  {
    "id": 3,
    "username": "Tom***test***",
    "mobile": "153******83",
    "email": "t****@baomidou.com"
  }
]

GET http://localhost:8080/list?skip=1

[
  {
    "id": 1,
    "username": "Jone",
    "mobile": "15315388881",
    "email": "test1@baomidou.com"
  },
  {
    "id": 2,
    "username": "Jack",
    "mobile": "15315388882",
    "email": "test2@baomidou.com"
  },
  {
    "id": 3,
    "username": "Tom",
    "mobile": "15315388883",
    "email": "test3@baomidou.com"
  }
]

来源:https://blog.csdn.net/weixin_61594803

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
离婚当天婆家摆庆功宴,我在暗处布下天罗网,结账时全家脸都绿了

离婚当天婆家摆庆功宴,我在暗处布下天罗网,结账时全家脸都绿了

城南雪花开
2026-02-25 20:33:23
沃什重创"美元贬值交易"!黄金崩了,比特币重挫,芯片狂欢还能撑多久?

沃什重创"美元贬值交易"!黄金崩了,比特币重挫,芯片狂欢还能撑多久?

华尔街见闻官方
2026-06-25 13:47:19
越闹越大!“走个面”事件讽刺漫画及对话出炉,双方“底裤”被扒

越闹越大!“走个面”事件讽刺漫画及对话出炉,双方“底裤”被扒

火山詩话
2026-06-25 06:37:37
终于有人为韩红发声!网友:只有北京人对她的这次拉票有评价资质

终于有人为韩红发声!网友:只有北京人对她的这次拉票有评价资质

火山詩话
2026-06-26 05:58:59
蒋涛:当一个人 28 天写完 Claude Code,开源开发者的红利去了哪里?

蒋涛:当一个人 28 天写完 Claude Code,开源开发者的红利去了哪里?

CSDN
2026-06-25 21:35:18
估分445查分519!达州一考生高考逆袭,全家激动欢呼致谢母校

估分445查分519!达州一考生高考逆袭,全家激动欢呼致谢母校

封面新闻
2026-06-26 00:08:02
李世民在HK风评很差?为何教科书上全是负面评价?

李世民在HK风评很差?为何教科书上全是负面评价?

小豫讲故事
2026-06-17 06:00:10
普京:乌克兰是前线撑不住才打俄本土

普京:乌克兰是前线撑不住才打俄本土

桂系007
2026-06-23 23:10:03
一碗隔夜米饭毒死7人?医生警告:米饭尽量别这样吃,比砒霜还毒

一碗隔夜米饭毒死7人?医生警告:米饭尽量别这样吃,比砒霜还毒

路医生健康科普
2026-05-31 20:15:03
印尼万万没想到,中方竟这么狠!曾收割中企想拿捏中国,却被反制

印尼万万没想到,中方竟这么狠!曾收割中企想拿捏中国,却被反制

牛锅巴小钒
2026-06-25 19:30:04
163的王祖蓝和175的老婆换裤子穿,一个成人行拖把,一个成7分裤

163的王祖蓝和175的老婆换裤子穿,一个成人行拖把,一个成7分裤

木子爱娱乐大号
2026-06-22 10:21:23
一眼就能看出“家境优渥”的中年女人,大多有3个特质,很准

一眼就能看出“家境优渥”的中年女人,大多有3个特质,很准

大熊欢乐坊
2026-06-25 18:21:14
泰国王室继承人杀出黑马,育坤家族王子血统纯正,也比提帮功聪明

泰国王室继承人杀出黑马,育坤家族王子血统纯正,也比提帮功聪明

照见古今
2026-06-25 18:02:39
龙泉山出现手臂粗菜花蛇,无毒蛇为何被称为百蛇之王?

龙泉山出现手臂粗菜花蛇,无毒蛇为何被称为百蛇之王?

普陀动物世界
2026-06-25 12:07:40
6-15年私家车年检重点!吃透这几点,再也不怕年检返工吃罚单

6-15年私家车年检重点!吃透这几点,再也不怕年检返工吃罚单

老特有话说
2026-06-26 16:23:30
癌症是基因注定的,躲也躲不掉?父母得了5种癌,或遗传给下一代

癌症是基因注定的,躲也躲不掉?父母得了5种癌,或遗传给下一代

39健康网
2026-06-25 08:30:42
67岁王朔现状:一身毛病,爱吃甜食,独自定居北京,拒绝综艺商演

67岁王朔现状:一身毛病,爱吃甜食,独自定居北京,拒绝综艺商演

陈意小可爱
2026-06-26 15:51:05
范志毅说的果然没错!人民日报锐评董路,犀利言论直击球迷心声

范志毅说的果然没错!人民日报锐评董路,犀利言论直击球迷心声

领悟看世界
2026-06-13 00:57:31
A妈直播摊牌!曝光和莫莉真实关系,直言压根不熟,全程只跟着杰森

A妈直播摊牌!曝光和莫莉真实关系,直言压根不熟,全程只跟着杰森

小椰的奶奶
2026-06-26 12:39:00
女演员千万别整容,看《问心2》毛晓彤与张佳宁同框,对比很惨烈

女演员千万别整容,看《问心2》毛晓彤与张佳宁同框,对比很惨烈

娱说瑜悦
2026-06-25 23:25:33
2026-06-26 17:23:00
Meta
Meta
关注java进阶架构师送架构
1076文章数 9851关注度
往期回顾 全部

科技要闻

美国政府要求OpenAI分批发布GPT-5.6

头条要闻

朝鲜领导层重大调整:"反腐少将"被查 赵甬元被"召回"

头条要闻

朝鲜领导层重大调整:"反腐少将"被查 赵甬元被"召回"

体育要闻

三球换里德:森林狼和黄蜂谁更癫?!

娱乐要闻

刘嘉玲想放弃梁朝伟,没有自理能力

财经要闻

悬在科技头上的达摩克利斯之剑

汽车要闻

老板们的新座驾!65万元起,尊界V800/V680开启预订

态度原创

数码
时尚
旅游
艺术
家居

数码要闻

Rokid AR眼镜亮相:骁龙至尊空间计算协处理器,空间+ AI双摄

盛夏,才要穿出松弛感!

旅游要闻

老一辈都听过的滇王故事,一座古庙串联起整个消失的古滇王国!

艺术要闻

470米!重庆“第一高楼”梦断?上架拍卖!

家居要闻

绿意盎然 自然之境

无障碍浏览 进入关怀版