当前位置: 代码迷 >> 综合 >> SpringAOP-@Around过滤敏感字,返回无结果集
  详细解决方案

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

热度:99   发布时间:2024-03-05 21:23:38.0

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

@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;}