java 通过注解、切面、反射实现返回信息脱敏

阅读 850 标签:java基础  java框架  
/**
 * projectName micro-util
 * package com.open.util.handler.aspect
 * className SensitiveMethod  
 * 
 * description: 标注于需要处理的方法上
 * 
 */
@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 {};
}
/**
 * projectName micro-util
 * package com.open.util.handler.aspect
 * className SensitiveMapMeta  
 * 
 * description: 标注于需要处理的方法上,当返回类型是map时标注
 * 
 *
 */
@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;
}
/**
 * projectName micro-util
 * package com.open.util.handler.aspect
 * className EncMeta  
 * 
 * description: 标注于需要加密的字段上
 * 
 *
 */
@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() {
    }
    /**
     * description: PARAMETER 不支持,所有加密的数据都放到body中
     * //        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 EncMeta && Objects.nonNull(arg) && arg instanceof String) {
     * //                    log.debug("Source args[{}] value {}", i, arg);
     * //                    args[i] = encManager.decField(String.valueOf(arg));
     * //                }
     * //            }
     * //        }
     *
     * @param point
     * @return {@link Object}
     * @throws
     */
    @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);
    }
}
文章来源:网络 版权归原作者所有,如涉及知识产权问题,请权利人联系我们,我们将立即处理.
此情可待追忆
文章 15 获得 0个赞 共 0个粉丝

推荐阅读 更多精彩内容