@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveMethod {
boolean enabled() default true;
boolean encParamEnabled() default false;
boolean encResultEnabled() default false;
boolean sensitiveResultEnabled() default true;
Class sensitiveClass() default Integer.class;
SensitiveMapMeta[] sensitiveMapMeta() default {};
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveMapMeta {
String key() default "";
Class sensitiveClass() default Integer.class;
boolean encResultEnabled() default false;
boolean sensitiveResultEnabled() default true;
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EncMeta {
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EncSubMeta {
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveMeta {
int rpStart() default 0;
int rpEnd() default 1;
String rpSymbol() default "*";
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveSubMeta {
}
@Slf4j
@Aspect
@Order(Ordered.LOWEST_PRECEDENCE - 100)
@Component
@ConditionalOnProperty(prefix = "open.advice.sensitive", name = "enabled", havingValue = "true")
public class SensitiveInfoAspect {
@Autowired
private List sensitiveManageChain = new CopyOnWriteArrayList<>();
@Autowired
private TransSensitiveFieldProvider transSensitiveField;
@Pointcut("@annotation(com.open.util.entity.annotation.SensitiveMethod)")
private void allAnnotationMethod() {
}
@Around("allAnnotationMethod()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
SensitiveMethod sensitiveMethodMeta = method.getAnnotation(SensitiveMethod.class);
if (sensitiveMethodMeta.encParamEnabled()) {
ArrayList decFields = new ArrayList<>();
Object[] args = point.getArgs();
Annotation[][] paramAnnotations = method.getParameterAnnotations();
for (int i = 0; i < paramAnnotations.length; i++) {
for (int j = 0; j < paramAnnotations[i].length; j++) {
Annotation currentAnnotation = paramAnnotations[i][j];
Object arg = args[i];
if (currentAnnotation instanceof RequestBody && Objects.nonNull(arg)) {
Field[] fields = arg.getClass().getDeclaredFields();
transSensitiveField.classifyFields(arg.getClass(), fields, decFields, null, null, null);
log.debug("Source args[{}] value {}", i, arg);
decFields.forEach(e -> transSensitiveField.decField(e, arg));
}
}
}
}
Object tempObj = point.proceed();
if (!sensitiveMethodMeta.enabled() || Objects.isNull(tempObj)) {
return tempObj;
}
List sortedChain = sensitiveManageChain.stream()
.sorted(Comparator.comparing(SensitiveManageChain::getOrder)).collect(Collectors.toList());
for (int i = 0; i < sortedChain.size() - 1; i++) {
sortedChain.get(i).setNextChain(sortedChain.get(i + 1));
}
return sortedChain.get(0).sensitiveHand(tempObj, method, sensitiveMethodMeta, null, new ArrayList<>(), new ArrayList<>(),
new ArrayList<>(), new ArrayList<>(), sortedChain);
}
}