欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于注解的字段脱敏处理

程序员文章站 2022-07-03 14:50:09
...
package io.lgxkdream.test2;

import org.apache.commons.lang3.StringUtils;

/**
 * @title: MaskUtils
 * @description: 掩码工具类
 * @Copyright: Copyright (c) 2018
 * @Company: lgxkdream.github.io
 * @author gang.li
 * @version 1.0.0
 * @since 2018年3月19日 上午10:59:57
 */
public class MaskUtils {
	public static String maskCellphone(String cellphoneNo) {
		if ((cellphoneNo == null) || (cellphoneNo.trim().length() != 11)) {
			return cellphoneNo;
		}
		return cellphoneNo.substring(0, 3) + "****" + cellphoneNo.substring(cellphoneNo.length() - 4);
	}

	public static String maskEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return "";
		}
		int index = StringUtils.indexOf(email, "@");
		if (index <= 1)
			return email;
		else
			return StringUtils.rightPad(StringUtils.left(email, 1), index, "*")
					.concat(StringUtils.mid(email, index, StringUtils.length(email)));
	}

	private static String maskCardNo(String cardNo) {
		if ((cardNo == null) || (cardNo.trim().length() <= 8)) {
			return cardNo;
		}
		cardNo = cardNo.trim();
		int length = cardNo.length();
		String firstFourNo = cardNo.substring(0, 4);
		String lastFourNo = cardNo.substring(length - 4);
		String mask = "";
		for (int i = 0; i < length - 8; i++) {
			mask = mask + "*";
		}
		return firstFourNo + mask + lastFourNo;
	}

	public static String maskIDCardNo(String idCardNo) {
		return maskCardNo(idCardNo);
	}

	public static String maskBankCardNo(String bankCardNo) {
		return maskCardNo(bankCardNo);
	}
	
}
package io.lgxkdream.test2;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @title: SensitiveField
 * @description: 掩码注解
 * @Copyright: Copyright (c) 2018
 * @Company: lgxkdream.github.io
 * @author gang.li
 * @version 1.0.0
 * @since 2018年3月19日 上午11:01:45
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveField {

	SensitiveTypeEnum value() default SensitiveTypeEnum.IDCARD;

    enum SensitiveTypeEnum {
        IDCARD,
        BANKCARD,
        MOBILE,
        EMAIL,
        CVV;
    }
    
}
package io.lgxkdream.test2;

import java.lang.reflect.Field;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @title: SensitiveReflectionToStringBuilder
 * @description: 重写用于toString的ReflectionToStringBuilder
 * @Copyright: Copyright (c) 2018
 * @Company: lgxkdream.github.io
 * @author gang.li
 * @version 1.0.0
 * @since 2018年3月19日 上午11:06:20
 */
public class SensitiveReflectionToStringBuilder extends ReflectionToStringBuilder {

	public SensitiveReflectionToStringBuilder(Object object, ToStringStyle style) {
		super(object, style);
	}

	@Override
	protected Object getValue(Field field) throws IllegalArgumentException, IllegalAccessException {
		if (field.getType() == String.class && field.isAnnotationPresent(SensitiveField.class)) {
			String v = (String) field.get(this.getObject());
			switch (field.getAnnotation(SensitiveField.class).value()) {
			case IDCARD:
				return MaskUtils.maskIDCardNo(v);
			case BANKCARD:
				return MaskUtils.maskBankCardNo(v);
			case MOBILE:
				return MaskUtils.maskCellphone(v);
			case EMAIL:
				return MaskUtils.maskEmail(v);
			case CVV:
				return v == null ? null : "***";
			default:
				return v;
			}
		}
		return field.get(this.getObject());
	}

}
package io.lgxkdream.test2;

import org.apache.commons.lang3.builder.ToStringStyle;

import io.lgxkdream.test2.SensitiveField.SensitiveTypeEnum;

/**
 * @title: MyEntity
 * @description: 测试实体
 * @Copyright: Copyright (c) 2018
 * @Company: lgxkdream.github.io
 * @author gang.li
 * @version 1.0.0
 * @since 2018年3月19日 上午11:21:04
 */
public class MyEntity {
	
	/**
     * 证件号
     */
    @SensitiveField
    private String idNo;

    /**
     * 手机号
     */
    @SensitiveField(SensitiveTypeEnum.MOBILE)
    private String mobile;

    /**
     * 姓名
     */
    private String name;
    
    /**
     * 邮箱
     */
    @SensitiveField(SensitiveTypeEnum.EMAIL)
    private String email;

	public String getIdNo() {
		return idNo;
	}

	public void setIdNo(String idNo) {
		this.idNo = idNo;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
    
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
    public String toString() {
        return new SensitiveReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
    }

}
package io.lgxkdream.test2;

/**
 * @title: MyTest
 * @description: 测试类
 * @Copyright: Copyright (c) 2018
 * @Company: lgxkdream.github.io
 * @author gang.li
 * @version 1.0.0
 * @since 2018年3月19日 上午11:21:22
 */
public class MyTest {
	
	public static void main(String[] args) {
		MyEntity entity = new MyEntity();
		entity.setIdNo("220222196309129847");
		entity.setMobile("15764132696");
		entity.setName("张三");
		entity.setEmail("[email protected]");
		System.out.println(entity.toString());
		// 测试结果:MyEntity[idNo=2202**********9847,mobile=157****2696,name=张三,email=2*********@qq.com]
	}

}



相关标签: 字段脱敏