springboot 之 数据“脱敏”加解密简单操作

依赖

<dependency>
  <groupId>com.github.yzcheng90</groupId>
  <artifactId>autofull-spring-boot-starter</artifactId>
  <version>1.3.5</version>
</dependency>

1、后台

    返回数据
1、先在数据实体类需要加密的字段上加上注解 @AutoFullMask
@Data
public class TestMaskEntity {
          
   

    @AutoFullMask(type = MaskType.phone)
    public String phone;


    @AutoFullMask(type = MaskType.idCard)
    public String idCard;

}
2、在controller方法上加上注解@AutoFullData
@AutoFullData
@RequestMapping(value = "/testResult",method = RequestMethod.GET)
public R testResult(){
          
   
    TestMaskEntity entity = new TestMaskEntity();
    entity.setPhone("13985745632");
    entity.setIdCard("431129199208082222");
    return R.ok().setData(entity);
}
3、返回数据如下
{
          
   
	"msg": "success",
	"code": 0,
	"data": {
          
   
    	"phone": "139****5632@suke@0dqrtG9yJIkzN4c6oqx73Q==",
    	"idCard": "431129********2222@suke@5t7h/p2GsjGyZlkJM7rQJQ=="
    }
}
    后台接收数据
请求数据如下
1、自动解密(在参数或方法上加上注解@AutoDecodeMask目前只对两种参数方式生效)
@AuthIgnore
@RequestMapping(value = "/testMask",method = RequestMethod.POST)
public TestMaskEntity testMask(@AutoDecodeMask TestMaskEntity param){
          
   
    return param;
}

@AuthIgnore
@RequestMapping(value = "/testMaskBody",method = RequestMethod.POST)
public TestMaskEntity testMaskBody(@RequestBody @AutoDecodeMask TestMaskEntity param){
          
   
    return param;
}
2、手动解密 使用工具类 DecodeMaskDataHandle.decode(phone)
@AuthIgnore
@RequestMapping(value = "/testMaskRequest",method = RequestMethod.POST)
public String testMaskRequest(HttpServletRequest request){
          
   
    String phone = request.getParameter("phone");
    return DecodeMaskDataHandle.decode(phone);
}

@AuthIgnore
@RequestMapping(value = "/testMaskParam",method = RequestMethod.POST)
public String testMaskParam(@RequestParam("phone") String phone){
          
   
    return DecodeMaskDataHandle.decode(phone);
}

2、前台

1、显示建议截取标识 @autofull@
phone.substring(0,phone.lastIndexOf("@autofull@"))
2、传到后台需要把密文带上,比如没有对加密的phone字段做任何修改,直接把原数据传给后台
{
          
   "phone": "139****5632@autofull@0dqrtG9yJIkzN4c6oqx73Q=="}
3、如果修改了,或者是新增的时候就直接传文明
{
          
   "phone": "13912345632"}
最后 项目地址:
经验分享 程序员 微信小程序 职场和发展