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"}

最后 项目地址:https://github.com/yzcheng90/autofull

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值