1. /**
    2. * 修饰函数和参数,用于属性的核查
    3. *
    4. * <p>
    5. * <ul>
    6. * <li>1.修饰类:则会核查类下面所有函数的所有参数</li>
    7. * <li>2.修饰函数:则会核查函数对应的所有参数</li>
    8. * <li>3.修饰参数:则只会核查指定的参数</li>
    9. * </ul>
    10. * @author robot
    11. */
    12. @Documented
    13. @Retention(RetentionPolicy.RUNTIME)
    14. @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
    15. public @interface AutoCheck {
    16. /**
    17. * 分组
    18. */
    19. String group() default "_default_";
    20. }
    1. /**
    2. * 添加该注解,则会在切面中,将修饰的方法的出参入参和耗时打印到info日志中
    3. *
    4. * @author robot
    5. */
    6. @Documented
    7. @Target({ElementType.METHOD, ElementType.TYPE})
    8. @Retention(RetentionPolicy.RUNTIME)
    9. public @interface EnableAopLog
    10. {}

    添加mikilin进行核查,也就是说跟这个结合

    1. /**
    2. * @author robot
    3. */
    4. @Slf4j
    5. @Aspect
    6. @Component
    7. public class ControllerAop {
    8. /**
    9. * 拦截方法中添加注解{@link EnableAopLog}的方法
    10. */
    11. @Around("@annotation(com.isyscore.walle.admin.aop.EnableAopLog)")
    12. public Object aroundEnableLog(ProceedingJoinPoint pjp) throws Throwable {
    13. Method currentMethod = getMethod(pjp);
    14. EnableAopLog enableAopLog = currentMethod.getDeclaredAnnotation(EnableAopLog.class);
    15. long start = System.currentTimeMillis();
    16. NeoMap outInfo = NeoMap.of();
    17. // 函数名字
    18. String funStr = pjp.getSignature().toLongString();
    19. outInfo.put("fun", funStr);
    20. // 参数的值
    21. outInfo.put("parameters", getParameters(pjp));
    22. Object result = null;
    23. try {
    24. result = pjp.proceed();
    25. outInfo.put("result", result);
    26. } catch (Exception e) {
    27. outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
    28. log.error(JSON.toJSONString(outInfo), e);
    29. return result;
    30. }
    31. outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
    32. if (enableAopLog.enable()) {
    33. log.info(JSON.toJSONString(outInfo));
    34. }
    35. return result;
    36. }
    37. /**
    38. * 拦截controller中所有的方法
    39. */
    40. @Around("execution(* com.isyscore.walle.admin.web.controller.*.*(..))")
    41. public Object aroundParameter(ProceedingJoinPoint pjp) {
    42. long start = System.currentTimeMillis();
    43. String funStr = pjp.getSignature().toLongString();
    44. Object result;
    45. Method currentMethod = getMethod(pjp);
    46. try {
    47. validate(pjp);
    48. result = pjp.proceed();
    49. } catch (Throwable e) {
    50. NeoMap outInfo = NeoMap.of();
    51. outInfo.put("fun", funStr);
    52. outInfo.put("parameters", getParameters(pjp));
    53. outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
    54. outInfo.put("errMsg", e.getMessage());
    55. log.error("后端异常:" + outInfo.toString(), e);
    56. Class<?> returnClass = currentMethod.getReturnType();
    57. if (e instanceof BusinessException) {
    58. if (Response.class.isAssignableFrom(returnClass)) {
    59. BusinessException businessException = (BusinessException) e;
    60. return Response.fail(businessException.getErrCode(), businessException.getMessage());
    61. } else {
    62. return null;
    63. }
    64. } else {
    65. if (Response.class.isAssignableFrom(returnClass)) {
    66. return Response.fail(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage());
    67. } else {
    68. return null;
    69. }
    70. }
    71. }
    72. return result;
    73. }
    74. private List<Object> getParameters(ProceedingJoinPoint pjp) {
    75. List<Object> parameters = new ArrayList<>();
    76. Object[] args = pjp.getArgs();
    77. for (Object arg : args) {
    78. if (arg instanceof ServletRequest || arg instanceof ServletResponse || arg instanceof MultipartFile) {
    79. continue;
    80. }
    81. parameters.add(arg);
    82. }
    83. return parameters;
    84. }
    85. @SuppressWarnings("all")
    86. private void validate(ProceedingJoinPoint pjp) {
    87. Signature sig = pjp.getSignature();
    88. MethodSignature methodSignature;
    89. if (!(sig instanceof MethodSignature)) {
    90. throw new IllegalArgumentException("该注解只能用于方法");
    91. }
    92. methodSignature = (MethodSignature) sig;
    93. Method currentMethod;
    94. try {
    95. currentMethod = pjp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
    96. } catch (NoSuchMethodException e) {
    97. throw new BusinessException(e);
    98. }
    99. if (currentMethod.getDeclaringClass().isAnnotationPresent(AutoCheck.class)) {
    100. doValidate(pjp);
    101. } else if (currentMethod.isAnnotationPresent(AutoCheck.class)) {
    102. doValidate(pjp);
    103. } else {
    104. Parameter[] parameters = currentMethod.getParameters();
    105. Object[] args = pjp.getArgs();
    106. for (int index = 0; index < args.length; index++) {
    107. if (args[index] instanceof ServletRequest || args[index] instanceof ServletResponse || args[index] instanceof MultipartFile) {
    108. continue;
    109. }
    110. if (parameters[index].isAnnotationPresent(AutoCheck.class)) {
    111. try {
    112. MkValidators.validate(args[index]);
    113. } catch (MkCheckException e) {
    114. throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.toString(), "参数核查异常:" + MkValidators.getErrMsg());
    115. }
    116. }
    117. }
    118. }
    119. }
    120. @SuppressWarnings("all")
    121. private void doValidate(ProceedingJoinPoint pjp) {
    122. Object[] parameters = pjp.getArgs();
    123. for (Object parameter : parameters) {
    124. try {
    125. MkValidators.validate(parameter);
    126. } catch (MkCheckException e) {
    127. String checkErr = "参数核查异常:" + MkValidators.getErrMsg();
    128. throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.toString(), checkErr);
    129. }
    130. }
    131. }
    132. private Method getMethod(ProceedingJoinPoint pjp) {
    133. Signature sig = pjp.getSignature();
    134. MethodSignature methodSignature;
    135. if (!(sig instanceof MethodSignature)) {
    136. throw new IllegalArgumentException("该注解只能用于方法");
    137. }
    138. methodSignature = (MethodSignature) sig;
    139. Method currentMethod;
    140. try {
    141. currentMethod = pjp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
    142. } catch (NoSuchMethodException e) {
    143. throw new BusinessException(e);
    144. }
    145. return currentMethod;
    146. }
    147. }