Spring boot 通过AOP获取参数名称及参数值并对参数进行校验
来源:互联网 发布:心理学考研 机构 知乎 编辑:程序博客网 时间:2024/05/25 19:58
/** * 定义切入点为 带有 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;}
阅读全文
0 0
- Spring boot 通过AOP获取参数名称及参数值并对参数进行校验
- spring aop参数校验
- Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理
- spring aop拦截Controller做参数校验
- Spring AOP注解的方式校验参数
- AOP完成参数校验
- 参数校验与aop
- spring aop 利用JoinPoint获取参数的值和方法名称
- Spring Boot项目使用参数校验
- Spring Boot项目使用参数校验
- Spring Boot -- Controller之Validate参数校验
- spring boot注解实现参数校验
- Spring-boot 配置Aop获取controller里的request中的参数以及其返回值
- Spring-boot 配置Aop获取controller里的request中的参数以及其返回值
- 自定义Spring的Aop切面类拦截业务请求,并获取到请求的参数名和参数值
- 使用Spring AOP对url参数进行解密以及对返回结果进行加密
- Spring LocalVariableTableParameterNameDiscoverer 获取方法的参数名称
- Spring 中 java 获取方法参数名称
- 排序详解:归并排序
- oracle中的排序问题
- ssh参数
- C++——【USACO 4.1.2】——Fence Loops
- nagios安装
- Spring boot 通过AOP获取参数名称及参数值并对参数进行校验
- [境内法规]中国人民银行令(2006)第2号—金融机构大额交易和可疑交易报告管理办法
- 如何给自已的应用程序添加命交互命令行
- 【SCOI2005】【codevs 2451】互不侵犯
- EasyUi日期控件datebox设置,只显示年月
- VisualVM
- Java开发 23种设计模式
- Android存储访问及目录
- Java构建工具:Ant vs Maven vs Gradle