处理AOP参数异常问题

This commit is contained in:
wenc000 2019-11-14 23:42:20 +08:00
parent 6856e98885
commit 2dbb18283e
3 changed files with 80 additions and 26 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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;