springboot mybatis 数据库敏感字段加密

  1. 引入jar包

<dependency>
    <groupId>io.github.panlicun</groupId>
    <artifactId>mybatisplus-database-encrypt-spring-boot-starter</artifactId>
    <version>1.0.6</version>
</dependency>
  1. 在yml配置文件中添加配置

enable:如果设置成false,则不启用加密

key:加密算法的key值,如果不填则使用默认的key值进行加密,为了安全起见建议更换。

type:目前工具的加密算法只支持aes、sm4两种,如果不指定类型则默认使用aes方式进行加密

encrypt:
  enable: true
  key: ENC(ypxav+oeh7K1U73uxaceEsPCy/3VGG8DHv+a8KhagvXkF7X7/oIB6w==)  #如果使用aes加密方式,key的长度只支持 16/24/32字节
  type: sm4  #目前只支持sm4和aes两种加密方式。如果不填则使用默认(aes)的加密方式
  1. 在实体类上实现Encrypted接口,并在要加密的字段上添加注解

@Data
@TableName(value = "test")
public class TestEntity implements Encrypted {

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @FieldEncrypt
    private String mobile;
}
  1. 正常调用接口及可以完成加密

mapper和service层是调用的mybatis-plus的通用mapper和service

public interface TestMapper extends BaseMapper<TestEntity> {
    
}
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, TestEntity> implements TestService {
}

controller层调用save方法插入数据

@GetMapping("/add")
public R add() {
    testService.save(new TestEntity().setMobile("123456"));
    return R.ok();
}

看一下数据库的数据

  1. 查询数据

  1. 支持mybatis-plus的单表查询:xxService.lambdaQuery() 、xxService.query()

  1. 支持原生mybatis mapper 查询,入参为单一对象的方法: User query(xxDTO dto)

  1. 原生mybatis mapper 查询,入参为字符串,如果入参为字符串需要在参数前面添加@Encrypt注解

    /**
     * 入参为字符串
     */
    @Select("select * from test where mobile=#{mobile}")
    TestEntity getByPhone1(@Encrypt @Param("mobile") String mobile);

    /**
     * 入参为对象
     */
    TestEntity getByPhone2(TestEntity test);
  1. 示例

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;


    @GetMapping("/get")
    public R get() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile","123456");
        List<TestEntity> list = testService.list(queryWrapper);
        return R.ok(list);
    }

    @GetMapping("/add")
    public R add() {
        testService.save(new TestEntity().setMobile("123456"));
        return R.ok();
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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实现脱敏处理的基本步骤。通过添加注解和自定义脱敏处理器,可以对敏感字段进行相应的脱敏操作,保护数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值