Starter实现SpringBoot数据脱敏

最终效果

"id":1,
"username":"姚**",
"password":"******",
"userMobile":"188****1345",
"userTel":"03556026586",
"userEmail":"c*******@aliyun.com",
"userBankNo":"6025 **** **** *** 2235",
"idnumber":"140428********0901"

1.导入starter依赖

        <dependency>
            <groupId>io.gitee.chemors</groupId>
            <artifactId>secure-ext-spring-boot-starter</artifactId>
            <version>1.0.3-RELEASE</version>
        </dependency>

2.创建用户实体类,使用@DesensitizationProp注解实现给数据自动脱敏

3.创建Handler数据访问,使用@Desensitization注解选择需要脱敏的访问路径

4.最后启动Springboot访问页面,得到数据

成功实现Starter方式的数据脱敏

Spring Boot是一个用于创建独立的、基于生产级别的Spring应用程序的框架。MyBatis是一个持久层框架,它可以与Spring Boot集成,用于简化数据库操作。脱敏处理是指在处理敏感数据时,对数据进行加密或者替换等操作,以保护数据的安全性。 在Spring Boot中,可以使用MyBatis-Plus来实现脱敏处理。MyBatis-Plus是MyBatis的增强工具,提供了一些方便的功能,包括脱敏处理。 要在Spring Boot实现脱敏处理,可以按照以下步骤进行操作: 1. 添加依赖:在`pom.xml`文件中添加MyBatis-Plus的依赖。 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 创建实体类:创建对应数据库表的实体类,并在需要脱敏的字段上添加注解。 ```java public class User { private Long id; @SensitiveInfo(type = SensitiveType.NAME) private String name; @SensitiveInfo(type = SensitiveType.PHONE) private String phone; // 其他字段... } ``` 3. 创建脱敏处理器:创建一个脱敏处理器,用于对敏感字段进行脱敏操作。 ```java public class SensitiveInfoHandler { public static String handle(String value, SensitiveType type) { // 根据不同的脱敏类型进行相应的处理 // 例如,对于姓名进行脱敏,可以将姓保留,将名替换为* if (type == SensitiveType.NAME) { return value.substring(0, 1) + "*"; } // 对于手机号进行脱敏,可以将中间四位替换为* else if (type == SensitiveType.PHONE) { return value.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); } // 其他脱敏处理... else { return value; } } } ``` 4. 创建脱敏处理器注解:创建一个注解,用于标记需要脱敏处理的字段。 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface SensitiveInfo { SensitiveType type(); } ``` 5. 创建脱敏处理器枚举:创建一个枚举类,用于定义脱敏类型。 ```java public enum SensitiveType { NAME, PHONE, // 其他脱敏类型... } ``` 6. 配置MyBatis-Plus:在`application.properties`或`application.yml`文件中配置MyBatis-Plus相关信息。 ```yaml mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 7. 编写Mapper和Service:编写对应的Mapper接口和Service类,使用MyBatis-Plus提供的方法进行数据库操作。 ```java public interface UserMapper extends BaseMapper<User> { // 自定义查询方法... } @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { // 自定义服务方法... } ``` 以上就是在Spring Boot中使用MyBatis-Plus实现脱敏处理的基本步骤。通过添加注解和自定义脱敏处理器,可以对敏感字段进行相应的脱敏操作,保护数据的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值