package org.demo.spring.mysql.dto;
import org.springframework.beans.BeanUtils;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class BaseDTO implements Serializable {
private static final long serialVersionUID = 25693154881567L;
//访问控制器,序列化的时候,不做处理
private transient ThreadLocal<BaseDTO> visitor = new ThreadLocal<BaseDTO>() {
protected BaseDTO initialValue() {
return null;
}
};
private String simpleName = this.getClass().getSimpleName();
public BaseDTO() {
}
/**
* 重写String
* 避免子类中相互引用引起输出toString时栈溢出
*
* @return
*/
private String toStringO() {
try {
PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(this.getClass());
Object[] params = new Object[0];
boolean isFirst = true;
StringBuilder strBuilder = new StringBuilder(512);
strBuilder.append(this.getClass().getName()).append('{');
PropertyDescriptor[] var9 = props;
int var10 = props.length;
for (int var11 = 0; var11 < var10; ++var11) {
PropertyDescriptor descriptor = var9[var11];
String filedName = descriptor.getName();
if ("class".equals(filedName)) continue;
Field field = this.getClass().getDeclaredField(descriptor.getName());
if (field != null) {
boolean fieldAccessible = field.isAccessible();
if (!fieldAccessible) {
field.setAccessible(true);
}
// 获取get方法
Method m = descriptor.getReadMethod();
if (m != null) {
boolean accessible = m.isAccessible();
if (!accessible) {
m.setAccessible(true);
}
try {
Object result = m.invoke(this, params);
// 获取属性上的注解
DesensitizationAnnotation maskAnnotationFi = (DesensitizationAnnotation) field.getAnnotation(DesensitizationAnnotation.class);
// get方法上的注解
DesensitizationAnnotation maskAnnotationMeth = (DesensitizationAnnotation) m.getAnnotation(DesensitizationAnnotation.class);
if (maskAnnotationFi != null || maskAnnotationMeth != null) {
DesensitizationAnnotation maskAnnotation = maskAnnotationFi == null ? maskAnnotationMeth : maskAnnotationFi;
/**
* 脱敏规则,默认***替换
* 也可以加入正则,实际打个日志没必要那么复杂
* 不过为了方便定位问题也不是不可以,
* 定位问题好像也没那么巧单单依赖脱敏字段,所以还是偷点懒吧
*
*/
result = maskAnnotation.desensitizationRule();
}
if (!isFirst) {
strBuilder.append(", ");
} else {
isFirst = false;
}
strBuilder.append(descriptor.getName()).append(": ");
if (result instanceof String) {
strBuilder.append('"');
strBuilder.append(result);
strBuilder.append('"');
} else {
strBuilder.append(result);
}
} catch (Exception var14) {
}
if (!accessible) {
m.setAccessible(false);
}
}
}
}
strBuilder.append('}');
return strBuilder.toString();
} catch (Exception var15) {
return super.toString();
}
}
public String toString() {
if (this.visitor.get() == null) {
this.visitor.set(this);
String var1;
try {
var1 = this.toStringO();
} finally {
this.visitor.set(null);
}
return var1;
} else {
return this.simpleName + "@" + Integer.toHexString(this.hashCode());
}
}
}
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DesensitizationAnnotation {
/**
* 脱敏规则,默认***替换
* @return
*/
public String desensitizationRule() default "***";
}