基於註解的欄位脫敏處理
阿新 • • 發佈:2018-11-14
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]
}
}