当前位置:   article > 正文

Springboot(SSM)项目实现数据脱敏_springboot 数据脱敏组件

springboot 数据脱敏组件

目录

一、引入hutool的依赖

二、sql脚本

三、自定义注解代码

3.1 自定义注解

3.2 自定义一个枚举,用于定义脱敏的类型

3.3 序列化

四、使用脱敏注解

4.1 Person.java

4.2 controller

4.3 dao

五、源代码参考


一、引入hutool的依赖

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.8.18</version>
  5. </dependency>

项目结构

 

二、sql脚本

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : 192.168.14.23
  4. Source Server Type : MySQL
  5. Source Server Version : 80031
  6. Source Host : 192.168.14.23:3306
  7. Source Schema : ss
  8. Target Server Type : MySQL
  9. Target Server Version : 80031
  10. File Encoding : 65001
  11. Date: 23/10/2023 15:43:05
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for person
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `person`;
  19. CREATE TABLE `person` (
  20. `id` int(0) NOT NULL AUTO_INCREMENT,
  21. `name` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  22. `idCard` varchar(18) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  23. `fixedtel` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  24. `tel` varchar(11) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  25. `address` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  26. `email` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  27. `password` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  28. `carid` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  29. `bankcard` varchar(19) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  30. `registertime` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  31. PRIMARY KEY (`id`) USING BTREE
  32. ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
  33. -- ----------------------------
  34. -- Records of person
  35. -- ----------------------------
  36. INSERT INTO `person` VALUES (1, 'zhang san', NULL, NULL, '15692536584', '河南省郑州市', NULL, '123654789', NULL, NULL, '2023-10-20 20:08:10');
  37. INSERT INTO `person` VALUES (2, 'zhang san', NULL, NULL, '15692536584', '河南省郑州市高新区科学大道108号', NULL, '123654789', NULL, NULL, '2023-10-20 20:19:52');
  38. INSERT INTO `person` VALUES (3, 'zhang san', NULL, NULL, '15692536584', '河南省郑州市高新区科学大道108号', NULL, '123654789', NULL, NULL, '2023-10-20 20:20:20');
  39. INSERT INTO `person` VALUES (4, 'zhang san', NULL, NULL, '15692536584', '河南省郑州市高新区科学大道108号', NULL, '123654789', NULL, NULL, '2023-10-20 20:23:42');
  40. INSERT INTO `person` VALUES (5, 'zhang san', '130169566636956632', NULL, '15692536584', '河南省郑州市高新区科学大道108号', NULL, '123654789', NULL, NULL, '2023-10-20 20:40:06');
  41. INSERT INTO `person` VALUES (6, '张三丰', '136956366695687746', NULL, '13211975672', '河南省郑州市高新区科学大道108号', '123456@163.com', 'sdfgsgsgsgaa ', '陕A184d5D', '6246605000005734228', '2023-10-20 21:19:27');
  42. SET FOREIGN_KEY_CHECKS = 1;

三、自定义注解代码

3.1 自定义注解

  1. package com.beiyou.annotation;
  2. import com.beiyou.enums.SensitizeRuleEnums;
  3. import com.beiyou.serializer.SensitiveJsonSerializer;
  4. import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
  5. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  6. import java.lang.annotation.ElementType;
  7. import java.lang.annotation.Retention;
  8. import java.lang.annotation.RetentionPolicy;
  9. import java.lang.annotation.Target;
  10. /**
  11. * @author 黄远超
  12. */
  13. @Retention(RetentionPolicy.RUNTIME)
  14. @Target(ElementType.FIELD)
  15. @JacksonAnnotationsInside
  16. //使用自定义的序列化方式
  17. @JsonSerialize(using = SensitiveJsonSerializer.class)
  18. public @interface Sensitize {
  19. SensitizeRuleEnums rule();
  20. }

3.2 自定义一个枚举,用于定义脱敏的类型

  1. package com.beiyou.enums;
  2. import cn.hutool.core.util.DesensitizedUtil;
  3. import java.util.function.Function;
  4. /**
  5. * @author 黄远超
  6. */
  7. public enum SensitizeRuleEnums {
  8. /**
  9. * 用户id脱敏
  10. */
  11. USER_ID(s -> String.valueOf(DesensitizedUtil.userId())),
  12. /**
  13. * 中文姓名脱敏
  14. */
  15. CHINESE_NAME(DesensitizedUtil::chineseName),
  16. /**
  17. * 身份证脱敏
  18. */
  19. ID_CARD(s -> DesensitizedUtil.idCardNum(s, 3, 4)),
  20. /**
  21. * 固定电话
  22. */
  23. FIXED_PHONE(DesensitizedUtil::fixedPhone),
  24. /**
  25. * 手机号脱敏
  26. */
  27. MOBILE_PHONE(DesensitizedUtil::mobilePhone),
  28. /**
  29. * 地址脱敏
  30. */
  31. ADDRESS(s -> DesensitizedUtil.address(s, 8)),
  32. /**
  33. * 电子邮箱脱敏
  34. */
  35. EMAIL(DesensitizedUtil::email),
  36. /**
  37. * 密码脱敏
  38. */
  39. PASSWORD(DesensitizedUtil::password),
  40. /**
  41. * 中国车牌脱敏
  42. */
  43. CAR_LICENSE(DesensitizedUtil::carLicense),
  44. /**
  45. * 银行卡脱敏
  46. */
  47. BANK_CARD(DesensitizedUtil::bankCard);
  48. private final Function<String, String> sensitize;
  49. public Function<String, String> sensitize() {
  50. return sensitize;
  51. }
  52. SensitizeRuleEnums(Function<String, String> sensitize) {
  53. this.sensitize = sensitize;
  54. }
  55. }

3.3 序列化

  1. package com.beiyou.serializer;
  2. import com.beiyou.annotation.Sensitize;
  3. import com.beiyou.enums.SensitizeRuleEnums;
  4. import com.fasterxml.jackson.core.JsonGenerator;
  5. import com.fasterxml.jackson.databind.BeanProperty;
  6. import com.fasterxml.jackson.databind.JsonMappingException;
  7. import com.fasterxml.jackson.databind.JsonSerializer;
  8. import com.fasterxml.jackson.databind.SerializerProvider;
  9. import com.fasterxml.jackson.databind.ser.ContextualSerializer;
  10. import java.io.IOException;
  11. import java.util.Objects;
  12. public class SensitiveJsonSerializer extends JsonSerializer<Object> implements ContextualSerializer {
  13. private SensitizeRuleEnums rule;
  14. @Override
  15. public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
  16. Sensitize annotation = beanProperty.getAnnotation(Sensitize.class);
  17. if (Objects.nonNull(annotation)) {
  18. this.rule = annotation.rule();
  19. return this;
  20. }
  21. return null;
  22. }
  23. @Override
  24. public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  25. jsonGenerator.writeString(rule.sensitize().apply(o.toString()));
  26. }
  27. }

四、使用脱敏注解

第三步的时候已经将自定义注解定义出来了,那么我们只需要在实体类中添加注解,并且通过枚举指定实体属性的脱敏类型即可。

4.1 Person.java

  1. package com.beiyou.model;
  2. import com.beiyou.annotation.Sensitize;
  3. import com.beiyou.enums.SensitizeRuleEnums;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Builder;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. @Data
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. @Builder
  12. public class Person {
  13. @Sensitize(rule = SensitizeRuleEnums.USER_ID)
  14. private Integer id;
  15. @Sensitize(rule = SensitizeRuleEnums.CHINESE_NAME)
  16. private String name;
  17. @Sensitize(rule = SensitizeRuleEnums.ID_CARD)
  18. private String idCard;
  19. @Sensitize(rule = SensitizeRuleEnums.FIXED_PHONE)
  20. private String fixedtel;
  21. @Sensitize(rule = SensitizeRuleEnums.MOBILE_PHONE)
  22. private String tel;
  23. @Sensitize(rule = SensitizeRuleEnums.ADDRESS)
  24. private String address;
  25. @Sensitize(rule = SensitizeRuleEnums.EMAIL)
  26. private String email;
  27. @Sensitize(rule = SensitizeRuleEnums.PASSWORD)
  28. private String password;
  29. @Sensitize(rule = SensitizeRuleEnums.ID_CARD)
  30. private String carid;
  31. @Sensitize(rule = SensitizeRuleEnums.BANK_CARD)
  32. private Long bankcard;
  33. }

4.2 controller

  1. package com.beiyou.controller;
  2. import com.beiyou.dao.PersonDao;
  3. import com.beiyou.model.Person;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.*;
  6. @RestController
  7. @RequestMapping("/api")
  8. public class PersonController {
  9. @Autowired
  10. PersonDao personDao;
  11. @PostMapping("/insert")
  12. public int insert(@RequestBody Person person) {
  13. return personDao.insert(person);
  14. }
  15. @GetMapping("getPerson")
  16. public Person getPerson() {
  17. Person person = new Person();
  18. person.setId(1555555550);
  19. person.setName("张三丰");
  20. person.setIdCard("16396583578");
  21. person.setTel("13211975672");
  22. person.setAddress("河南省郑州市高新区科学大道108号");
  23. person.setEmail("123456@163.com");
  24. person.setPassword("123456");
  25. person.setCarid("冀A888888");
  26. person.setBankcard(6246605000005734228L);
  27. return person;
  28. }
  29. @GetMapping("getPerson1")
  30. public Person getPerson1(Integer id) {
  31. return personDao.select(id);
  32. }
  33. }

4.3 dao

  1. package com.beiyou.dao;
  2. import com.beiyou.model.Person;
  3. import org.apache.ibatis.annotations.Insert;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Select;
  6. @Mapper
  7. public interface PersonDao {
  8. @Insert("insert person(name,IdCard,fixedtel,tel,address,email,password,carid,bankcard) values (#{name},#{idCard},#{fixedtel},#{tel},#{address},#{email},#{password},#{carid},#{bankcard})")
  9. int insert(Person person);
  10. @Select("select id,name,IdCard,fixedtel,tel,address,email,password,carid,bankcard from person where id = #{id}")
  11. Person select (Integer id);
  12. }

 

五、源代码参考

阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/920834
推荐阅读
相关标签
  

闽ICP备14008679号