SpringAOP-@Around过滤敏感字,返回无结果集

不想重复写代码,学会写笔记

@Aspect
@Service
@Slf4j
public class SpringBootAOP {

    //禁止查询商品名-切点
    @Pointcut("execution(* com.包路径.类名.方法名(..))")
    public void oneExcuteService() {
        //我是一个切点,然而我什么都没干
    }

    @Pointcut("execution(* com.包路径.类名.方法名(..))")
    public void twoExcuteService() {
        //我是第二个切点,然而我什么都没干
    }

    @Around(value = "oneExcuteService() || twoExcuteService()")
    public Object checkParams(ProceedingJoinPoint joinPoint) throws Throwable {
        String names = "会员";
                if (StringUtils.isBlank(names)) return joinPoint.proceed();
        List<String> nameList = Arrays.asList(names.split(","));
        //我是一个环绕,我要干点大事
        //获取请求信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 全路径类名
        String target = joinPoint.getSignature().getDeclaringTypeName();
        // 类名截取
        String classNm = target.substring(target.lastIndexOf(".") + 1);
        // 方法名
        String method = joinPoint.getSignature().getName();
        // 获取请求参数(json格式这么取简单使用,body里的去看下面的方法)
        Object[] objects = joinPoint.getArgs();
        if (objects.length < 1) return joinPoint.proceed();
        String paramsStr = objects[0].toString();

        try {
            List<Integer> count = Lists.newArrayList();
            nameList.forEach(item -> {
                if (paramsStr.indexOf(item) != -1) {
                    count.add(1);
                }
            });
            //如果含有敏感字结果集null返回
            if (count.size() > 0) {
                return null;
            }
        } catch (Exception e) {
            log.info("{}.{} 接收参数: {}, names : {}", classNm, method, JSONObject.toJSONString(objects[0]), names);
            log.error("Springboot - Aop - checkParams ERROR e ={}", ExceptionUtils.getMessage(e));
        }
        //放行无修改
        return joinPoint.proceed();
    }

}

/**
     * 获取请求参数
     */
    public static Map<String, String> getAllRequestParam(HttpServletRequest request) {
        Map<String, String> res = new HashMap<>();
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (temp.hasMoreElements()) {
                String en = (String) temp.nextElement();
                String value = request.getParameter(en);
                res.put(en, value);
                // 在报文上送时,如果字段的值为空,则不上送<下面的处理为在获取所有参数数据时,判断若值为空,则删除这个字段>
                if (StringUtils.isEmpty(res.get(en))) {
                    res.remove(en);
                }
            }
        }
        return res;
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无厘头の码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值