Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
lzhpo committed Nov 14, 2023
1 parent 66a4138 commit b19d67e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 67 deletions.
77 changes: 34 additions & 43 deletions src/main/java/com/lzhpo/sensitive/SensitiveStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReflectUtil;
import com.lzhpo.sensitive.annocation.Sensitive;
import com.lzhpo.sensitive.annocation.SensitiveFilterWords;
import com.lzhpo.sensitive.annocation.SensitiveHandler;
import com.lzhpo.sensitive.annocation.SensitiveKeepLength;
Expand All @@ -39,91 +38,82 @@ public enum SensitiveStrategy {
/** Chinese name */
CHINESE_NAME() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.chineseName(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.chineseName(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** ID card */
ID_CARD() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.idCardNum(sensitiveWrapper.getFieldValue(), 1, 2, sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.idCardNum(wrapper.getFieldValue(), 1, 2, wrapper.getReplacer());
}
},

/** Fixed phone */
FIXED_PHONE() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.fixedPhone(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.fixedPhone(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Mobile phone */
MOBILE_PHONE() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.mobilePhone(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.mobilePhone(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Address */
ADDRESS() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.address(sensitiveWrapper.getFieldValue(), 8, sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.address(wrapper.getFieldValue(), 8, wrapper.getReplacer());
}
},

/** Email */
EMAIL() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.email(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.email(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Password */
PASSWORD() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.password(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.password(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Chinese mainland license plates, including ordinary vehicles, new energy vehicles */
CAR_LICENSE() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.carLicense(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.carLicense(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Bank card */
BANK_CARD() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Sensitive sensitive = sensitiveWrapper.getSensitive();
return SensitiveUtil.bankCard(sensitiveWrapper.getFieldValue(), sensitive.replacer());
public String apply(SensitiveWrapper wrapper) {
return SensitiveUtil.bankCard(wrapper.getFieldValue(), wrapper.getReplacer());
}
},

/** Customize sensitive keep length */
CUSTOMIZE_FILTER_WORDS() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
String fieldValue = sensitiveWrapper.getFieldValue();
Field field = sensitiveWrapper.getField();
Sensitive sensitive = sensitiveWrapper.getSensitive();
public String apply(SensitiveWrapper wrapper) {
Object object = wrapper.getObject();
String fieldValue = wrapper.getFieldValue();
Field field = ReflectUtil.getField(object.getClass(), wrapper.getFieldName());
SensitiveFilterWords filterWords = field.getAnnotation(SensitiveFilterWords.class);
if (ObjectUtils.isEmpty(filterWords)) {
log.warn(
Expand All @@ -133,7 +123,7 @@ public String apply(SensitiveWrapper sensitiveWrapper) {
return fieldValue;
}

char replacer = sensitive.replacer();
char replacer = wrapper.getReplacer();
String[] words = filterWords.value();
if (!ObjectUtils.isEmpty(words)) {
for (String filterWord : words) {
Expand All @@ -151,10 +141,10 @@ public String apply(SensitiveWrapper sensitiveWrapper) {
/** Customize sensitive keep length */
CUSTOMIZE_KEEP_LENGTH() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Field field = sensitiveWrapper.getField();
String fieldValue = sensitiveWrapper.getFieldValue();
Sensitive sensitive = sensitiveWrapper.getSensitive();
public String apply(SensitiveWrapper wrapper) {
Object object = wrapper.getObject();
String fieldValue = wrapper.getFieldValue();
Field field = ReflectUtil.getField(object.getClass(), wrapper.getFieldName());
SensitiveKeepLength sensitiveKeepLength = field.getAnnotation(SensitiveKeepLength.class);
int preKeep = sensitiveKeepLength.preKeep();
int postKeep = sensitiveKeepLength.postKeep();
Expand All @@ -167,28 +157,29 @@ public String apply(SensitiveWrapper sensitiveWrapper) {
return fieldValue;
}

char replacer = sensitive.replacer();
char replacer = wrapper.getReplacer();
return CharSequenceUtil.replace(fieldValue, preKeep, fieldValue.length() - postKeep, replacer);
}
},

/** Customize sensitive handler */
CUSTOMIZE_HANDLER() {
@Override
public String apply(SensitiveWrapper sensitiveWrapper) {
Field field = sensitiveWrapper.getField();
public String apply(SensitiveWrapper wrapper) {
Object object = wrapper.getObject();
Field field = ReflectUtil.getField(object.getClass(), wrapper.getFieldName());
SensitiveHandler customizeHandler = field.getAnnotation(SensitiveHandler.class);
Class<? extends CustomizeSensitiveHandler> handlerClass = customizeHandler.value();
CustomizeSensitiveHandler handler = ReflectUtil.newInstance(handlerClass);
return handler.customize(sensitiveWrapper);
return handler.customize(wrapper);
}
};

/**
* Field sensitive strategy method
*
* @param sensitiveWrapper sensitive require message
* @param wrapper {@link SensitiveWrapper}
* @return after sensitive value
*/
public abstract String apply(SensitiveWrapper sensitiveWrapper);
public abstract String apply(SensitiveWrapper wrapper);
}
22 changes: 16 additions & 6 deletions src/main/java/com/lzhpo/sensitive/SensitiveWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.lzhpo.sensitive;

import com.lzhpo.sensitive.annocation.Sensitive;
import java.lang.reflect.Field;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -28,12 +27,23 @@
@AllArgsConstructor
public class SensitiveWrapper {

/** 字段 */
private Field field;
/**
* Current object.
*/
private Object object;

/** 字段值 */
/**
* Current field name.
*/
private String fieldName;

/**
* Current field value.
*/
private String fieldValue;

/** {@link Sensitive}注解信息 */
private Sensitive sensitive;
/**
* {@link Sensitive#replacer()}
*/
private char replacer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ protected Object process(Object object, String fieldName, Object fieldValue) {
return fieldValue;
}

Class<?> clazz = object.getClass();
Field field = ReflectUtil.getField(clazz, fieldName);
Field field = ReflectUtil.getField(object.getClass(), fieldName);
Sensitive sensitive = field.getAnnotation(Sensitive.class);
if (Objects.isNull(sensitive)) {
return fieldValue;
}

SensitiveStrategy strategy = sensitive.strategy();
log.debug("Sensitive for {} with {} strategy, replacer={}", fieldName, strategy.name(), sensitive.replacer());
return strategy.apply(new SensitiveWrapper(field, (String) fieldValue, sensitive));
return strategy.apply(new SensitiveWrapper(object, fieldName, (String) fieldValue, sensitive.replacer()));
}
// spotless:on
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void serialize(String fieldValue, JsonGenerator gen, SerializerProvider s
}

SensitiveStrategy strategy = sensitive.strategy();
String finalValue = strategy.apply(new SensitiveWrapper(field, fieldValue, sensitive));
String finalValue = strategy.apply(new SensitiveWrapper(object, fieldName, fieldValue, sensitive.replacer()));
log.debug("Sensitive for {} with {} strategy, replacer={}", fieldName, strategy.name(), sensitive.replacer());
gen.writeString(finalValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public String hello() {
return "hello";
}

@IgnoreSensitive({"name", "email"})
@GetMapping("sensitive")
public ResponseEntity<SensitiveEntity> sensitive() {
return ResponseEntity.ok(MockHelper.sensitive());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,15 @@

import com.lzhpo.sensitive.CustomizeSensitiveHandler;
import com.lzhpo.sensitive.SensitiveWrapper;
import com.lzhpo.sensitive.annocation.Sensitive;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import lombok.extern.slf4j.Slf4j;

/** @author lzhpo */
@Slf4j
public class FaceCustomizeSensitiveHandler implements CustomizeSensitiveHandler {

@Override
public String customize(SensitiveWrapper sensitiveWrapper) {
// 字段
Field field = sensitiveWrapper.getField();
// 字段归属的对象
Class<?> objectClass = field.getDeclaringClass();
// 字段上的注解
Annotation[] annotations = field.getAnnotations();
// 字段值
String fieldValue = sensitiveWrapper.getFieldValue();
// 注解信息
Sensitive sensitive = sensitiveWrapper.getSensitive();
public String customize(SensitiveWrapper wrapper) {
log.debug("sensitiveWrapper: {}", wrapper);
return "@#@";
}
}

0 comments on commit b19d67e

Please sign in to comment.