后端接口返回的数据或者日志记录时,因为安全隐私的缘故难免要对字符串脱敏处理,今天利用注解写个简单的脱敏工具。
1.字符串脱敏方法
package com.util;
public class CommonUtil {
/**
*
* @param noMosaicLength 不打码字符长度,
* @param minMosaicLength 最小打码字符长度
* @param inputString
* @return
*/
public static String mosaic(int noMosaicLength,int minMosaicLength,String inputString){
if(inputString == null || inputString.equals("")){
return inputString;
}
int noMosaicPart1Length;
int beMosaicLength = minMosaicLength;
if(inputString.length() <= minMosaicLength){
//保留首字符不打码
noMosaicPart1Length = 1;
beMosaicLength = inputString.length()-noMosaicPart1Length;
}else{
// 不打码长度加上最小打码长度大于输入字符串长度,则保证至少有最小打码个数字符被打码
if(noMosaicLength+minMosaicLength >= inputString.length()){
int singleSideNoMosaicLength = (inputString.length()-minMosaicLength)/2;
noMosaicPart1Length=(inputString.length()-minMosaicLength)%2 == 0 ? singleSideNoMosaicLength
:singleSideNoMosaicLength+1;
}else{
noMosaicPart1Length = noMosaicLength%2 == 0 ? noMosaicLength/2:noMosaicLength/2+1;
beMosaicLength = inputString.length()-noMosaicLength;
}
}
StringBuilder stringBuilder = new StringBuilder(inputString.substring(0,noMosaicPart1Length));
for(int i=0;i<beMosaicLength;i++){
stringBuilder.append("*");
}
if(noMosaicPart1Length+beMosaicLength<inputString.length()){
stringBuilder.append(inputString.substring(noMosaicPart1Length+beMosaicLength));
}
return stringBuilder.toString();
}
}
2.注解相关类
package com.annotation;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Mosaic {
int noMosaicLength() default 0;
MosaicType MOSAIC_TYPE() default MosaicType.INDICATE_MOSAIC_LENGTH;
int minMosaicLength() default 3;
}
打码类型枚举类
package com.annotation;
public enum MosaicType {
/**
* 完全打码
*/
FULL_MOSAIC(0),
/**
* 指定打码字符个数
*/
INDICATE_MOSAIC_LENGTH(1),
;
private int mosaicType;
MosaicType(int mosaicType){
this.mosaicType = mosaicType;
}
}
3.注解解析方法
package com.reflect;
import com.annotation.table.Mosaic;
import com.annotation.table.MosaicType;
import com.ly.entity.Student;
import com.util.CommonUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class MosaicUtil {
public static <T> T getMosaicObj(Object obj,Class<T> tClass) throws IllegalAccessException {
Class<?> objClazz = obj.getClass();
Field[] fields = objClazz.getDeclaredFields();
for(Field field:fields){
field.setAccessible(true);
Mosaic mosaic = field.getAnnotation(Mosaic.class);
Object object = field.get(obj);
if(mosaic != null && object != null){
if(object.getClass().isAssignableFrom(String.class)){
Object mosaicString = mosaicString((String)object,mosaic);
field.set(obj, mosaicString);
}
}
}
return (T) obj;
}
public static Object mosaicString(String src,Mosaic mosaic){
String des= "";
if(mosaic.MOSAIC_TYPE().equals(MosaicType.FULL_MOSAIC)){
des = CommonUtil.mosaic(0,src.length()-1,src);
}else if(mosaic.MOSAIC_TYPE().equals(MosaicType.FULL_MOSAIC)){
des= CommonUtil.mosaic(mosaic.noMosaicLength(),mosaic.minMosaicLength(),
src);
}
return des;
}
}
4.用法
package com.ly.entity;
import com.annotation.table.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Mosaic(noMosaicLength = 2,minMosaicLength = 3,MOSAIC_TYPE = MosaicType.FULL_MOSAIC)
private String name;
private int age;
private Teacher t;
public Student() {
super();
}
public Student(String name,int age) {
super();
this.name = name;
this.age = age;
}
public Student(String string, Object object) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getT() {
return t;
}
public void setT(Teacher t) {
this.t = t;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", t=" + t + "]";
}
public static void main(String[] args){
Student student = new Student();
student.setName("qwerty123");
System.out.println(MosaicUtil.getMosaicObj(student,Student.class));
}
}