From 2dbb18283e1cf8767042f2c302f40c40ad39b54f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Thu, 14 Nov 2019 23:42:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86AOP=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/common/aspect/ApiLogAspect.java | 75 +++++++++++++------ .../com/cm/common/aspect/ApiParamsAspect.java | 29 ++++++- .../exception/base/SystemException.java | 2 +- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java index 3fd307e..9016baa 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java @@ -64,7 +64,19 @@ public class ApiLogAspect { } /** - * 日志环绕 + * app切入点 + */ + @Pointcut("execution(public * com.cm.*.controller.app..*.*(..))") + public void appCutPoint() { + } + + @Around("appCutPoint()") + public Object appLogAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + return logAroundForApp(proceedingJoinPoint); + } + + /** + * api日志环绕 * * @param proceedingJoinPoint * @return @@ -72,9 +84,43 @@ public class ApiLogAspect { private Object logAroundForApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { HttpServletRequest request = RequestUtil.getRequest(); Signature signature = proceedingJoinPoint.getSignature(); + StringBuilder paramValue = getParams(proceedingJoinPoint); + return apiResult(proceedingJoinPoint, request, signature, paramValue); + } + + /** + * resource日志环绕 + * + * @param proceedingJoinPoint + * @return + * @throws Throwable + */ + private Object logAroundForResource(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + HttpServletRequest request = RequestUtil.getRequest(); + String accessToken = request.getParameter("access_token"); + Signature signature = proceedingJoinPoint.getSignature(); + StringBuilder paramValue = getParams(proceedingJoinPoint); + return resourceAndAppResult(proceedingJoinPoint, request, signature, paramValue, accessToken); + } + + /** + * app日志环绕 + * + * @param proceedingJoinPoint + * @return + * @throws Throwable + */ + private Object logAroundForApp(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + HttpServletRequest request = RequestUtil.getRequest(); + String accessToken = request.getHeader("accessToken"); + Signature signature = proceedingJoinPoint.getSignature(); + StringBuilder paramValue = getParams(proceedingJoinPoint); + return resourceAndAppResult(proceedingJoinPoint, request, signature, paramValue, accessToken); + } + + private StringBuilder getParams(ProceedingJoinPoint proceedingJoinPoint) { StringBuilder paramValue = new StringBuilder(); Object[] args = proceedingJoinPoint.getArgs(); - for (Object arg : args) { // 去除无效的参数 if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) { @@ -89,6 +135,10 @@ public class ApiLogAspect { e.printStackTrace(); } } + return paramValue; + } + + private Object apiResult(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request, Signature signature, StringBuilder paramValue) throws Throwable { Object result; try { long startTime = System.currentTimeMillis(); @@ -128,26 +178,7 @@ public class ApiLogAspect { return result; } - private Object logAroundForResource(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { - HttpServletRequest request = RequestUtil.getRequest(); - String accessToken = request.getParameter("accessToken"); - Signature signature = proceedingJoinPoint.getSignature(); - StringBuilder paramValue = new StringBuilder(); - Object[] args = proceedingJoinPoint.getArgs(); - for (Object arg : args) { - // 去除无效的参数 - if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) { - continue; - } - if (paramValue.length() > 0) { - paramValue.append("_wg_"); - } - try { - paramValue.append(JSON.toJSONString(arg)); - } catch (Exception e) { - e.printStackTrace(); - } - } + private Object resourceAndAppResult(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request, Signature signature, StringBuilder paramValue, String accessToken) throws Throwable { Object result; long startTime = System.currentTimeMillis(); result = proceedingJoinPoint.proceed(); diff --git a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java index d16c972..91d6be5 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java @@ -8,6 +8,7 @@ import com.google.inject.internal.cglib.core.$ClassInfo; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @@ -36,12 +37,34 @@ import java.util.List; public class ApiParamsAspect { private static final Logger LOG = LoggerFactory.getLogger(ApiParamsAspect.class); - @Pointcut("execution(public * com.cm..*Controller.*(..))") - public void controllerCutPoint() { + @Pointcut("execution(public * com.cm.*.controller..*.*(..))") + public void apiCutPoint() { } - @Before("controllerCutPoint()") + @Pointcut("execution(public * com.cm.common.plugin.controller..*.*(..))") + public void pluginCutPoint() { + } + + @Pointcut("execution(public * com.cm.common.plugin.oauth.controller..*.*(..))") + public void oauthCutPoint() { + } + + @Before("apiCutPoint()") public void beforeApiCutPoint(JoinPoint joinPoint) throws ParamsException { + beforeCutPoint(joinPoint); + } + + @Before("pluginCutPoint()") + public void beforePluginCutPoint(JoinPoint joinPoint) throws ParamsException { + beforeCutPoint(joinPoint); + } + + @Before("oauthCutPoint()") + public void beforeOauthCutPoint(JoinPoint joinPoint) throws ParamsException { + beforeCutPoint(joinPoint); + } + + private void beforeCutPoint(JoinPoint joinPoint) throws ParamsException { Signature signature = joinPoint.getSignature(); Object[] args = joinPoint.getArgs(); Class targetClazz = joinPoint.getTarget().getClass(); diff --git a/cloud-common/src/main/java/com/cm/common/exception/base/SystemException.java b/cloud-common/src/main/java/com/cm/common/exception/base/SystemException.java index b146937..10075ae 100644 --- a/cloud-common/src/main/java/com/cm/common/exception/base/SystemException.java +++ b/cloud-common/src/main/java/com/cm/common/exception/base/SystemException.java @@ -7,7 +7,7 @@ package com.cm.common.exception.base; * @Date: 2019/3/4 6:04 PM * @Version: 1.0 **/ -public class SystemException extends Exception { +public class SystemException extends RuntimeException { private boolean withMsg = false;