首页>代码>spring boot+mybatis学习资源共享平台系统代码分享>/resource-share-platform/resource-sharing-platform-java/src/main/java/com/cxs/aspect/SensitiveFilterAspect.java
package com.cxs.aspect;

import com.cxs.utils.DFAUtil;
import com.cxs.utils.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;

import java.lang.reflect.Field;
import java.util.Set;
import java.util.stream.Collectors;

/*
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Aspect
@Component
@Order(3)
public class SensitiveFilterAspect {

    @Value("${sensitive.config.template}")
    private String template;

    @Autowired
    private DFAUtil dfaUtil;

    @Pointcut("@annotation(com.cxs.aspect.annotation.SensitiveFilter)")
    public void pointcut(){

    }
    @Around(value = "pointcut()")
    public Object sensitiveFilter(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        for (Object arg : args) {
            // 跳过BindingResult
            if (arg instanceof BindingResult) {
                continue;
            }
            Class<?> clazz = arg.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(arg);
                if (!ObjectUtils.isEmpty(value)){
                    Set<String> map = dfaUtil.getSensitiveWordByDFAMap(value.toString(), 2);
                    if (!CollectionUtils.isEmpty(map)) {
                        Set<String> limit = map.stream()
                                .limit(4)
                                .collect(Collectors.toSet());
                        String msg = String.join(",", limit);
                        Result<String> result = new Result<>();
                        result.setCode(201).setMsg(template.replace("[1]", msg));
                        return result;
                    }
                }
            }
        }
        return point.proceed();
    }
}
最近下载更多
WenMeng  LV9 2024年12月17日
zolscy  LV24 2024年11月26日
lilong007  LV23 2024年9月3日
qiangmin1223  LV12 2024年4月24日
玖零定制问题修复  LV34 2024年4月4日
wanglinddad  LV55 2024年3月31日
15103432984  LV2 2024年3月17日
Demo1111  LV30 2023年12月7日
zyzyhh  LV2 2023年11月22日
最近浏览更多
newhaijun  LV16 6月7日
crung2759 2月10日
暂无贡献等级
WenMeng  LV9 2024年12月17日
yimaoermao  LV1 2024年11月28日
jugking 2024年11月1日
暂无贡献等级
yyhrhv  LV8 2024年9月3日
xiao小果  LV13 2024年8月29日
abdkfksdkf  LV16 2024年8月25日
waxy_123  LV2 2024年8月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友