1. 程式人生 > >Spring boot 通過AOP獲取引數名稱及引數值並對引數進行校驗

Spring boot 通過AOP獲取引數名稱及引數值並對引數進行校驗

/**
 * 定義切入點為 帶有 NotEnableEmpty 註解的 
 */
@Pointcut("@annotation(cn.acsm.paddy.manage.anotation.NotEnableEmpty)")
public void params() {
}

/**
 * 定義環繞通知
 * @param joinPoint
* @throws ClassNotFoundException
 * @throws NotFoundException
 * @throws IOException
 */
@Around("params()")
public void doBefore(ProceedingJoinPoint joinPoint) throws 
ClassNotFoundException, NotFoundException, IOException { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); HttpServletResponse response = attributes.getResponse(); String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType); String clazzName = clazz.getName(); String methodName = joinPoint.getSignature().getName(); //獲取方法名稱 Object[] args = joinPoint.getArgs();//引數 Map<String, Object> nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName, args);//獲取被切引數名稱及引數值
System.out.println(nameAndArgs.toString()); List<Object> requestParams = RequestParamsContextHolder.getRequestParamsLocal();//獲取不能為空的引數 RpcResponseEntity rpcResponseEntity = new RpcResponseEntity(); boolean hasEmptyParams = false; for (Object o : requestParams) { if (nameAndArgs.containsKey(o)) { Object o1 = nameAndArgs.get(o); if (o1 == null) { rpcResponseEntity.setInvoke_result_code(RpcResponseEnum.LACK_NECESSARY_PARAMS.getCode()); rpcResponseEntity.setInvoke_result_message(RpcResponseEnum.LACK_NECESSARY_PARAMS.getMessage()); hasEmptyParams = true; break; } } } if (hasEmptyParams) { response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "text/plain;charset=UTF-8"); response.setHeader("icop-content-type", "exception"); OutputStream stream = response.getOutputStream(); net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(rpcResponseEntity); stream.write(jsonObject.toString().getBytes("UTF-8")); } //獲取引數名稱和值 logger.info("當前呼叫介面-[" + request.getRequestURL() + "]"); } /** * 通過反射機制 獲取被切引數名以及引數值 * * @param cls * @param clazzName * @param methodName * @param args * @return * @throws NotFoundException */ private Map<String, Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException { Map<String, Object> map = new HashMap<String, Object>(); ClassPool pool = ClassPool.getDefault(); //ClassClassPath classPath = new ClassClassPath(this.getClass()); ClassClassPath classPath = new ClassClassPath(cls); pool.insertClassPath(classPath); CtClass cc = pool.get(clazzName); CtMethod cm = cc.getDeclaredMethod(methodName); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null) { // exception } // String[] paramNames = new String[cm.getParameterTypes().length]; int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < cm.getParameterTypes().length; i++) { map.put(attr.variableName(i + pos), args[i]);//paramNames即引數名 } return map; }