不想重复写代码,学会写笔记
@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();
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);
}
});
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;
}