处理AOP参数异常问题
This commit is contained in:
parent
6856e98885
commit
2dbb18283e
@ -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
|
* @param proceedingJoinPoint
|
||||||
* @return
|
* @return
|
||||||
@ -72,9 +84,43 @@ public class ApiLogAspect {
|
|||||||
private Object logAroundForApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
private Object logAroundForApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||||
HttpServletRequest request = RequestUtil.getRequest();
|
HttpServletRequest request = RequestUtil.getRequest();
|
||||||
Signature signature = proceedingJoinPoint.getSignature();
|
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();
|
StringBuilder paramValue = new StringBuilder();
|
||||||
Object[] args = proceedingJoinPoint.getArgs();
|
Object[] args = proceedingJoinPoint.getArgs();
|
||||||
|
|
||||||
for (Object arg : args) {
|
for (Object arg : args) {
|
||||||
// 去除无效的参数
|
// 去除无效的参数
|
||||||
if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) {
|
if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) {
|
||||||
@ -89,6 +135,10 @@ public class ApiLogAspect {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return paramValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object apiResult(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request, Signature signature, StringBuilder paramValue) throws Throwable {
|
||||||
Object result;
|
Object result;
|
||||||
try {
|
try {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
@ -128,26 +178,7 @@ public class ApiLogAspect {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object logAroundForResource(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
private Object resourceAndAppResult(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request, Signature signature, StringBuilder paramValue, String accessToken) 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Object result;
|
Object result;
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
result = proceedingJoinPoint.proceed();
|
result = proceedingJoinPoint.proceed();
|
||||||
|
@ -8,6 +8,7 @@ import com.google.inject.internal.cglib.core.$ClassInfo;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.Signature;
|
import org.aspectj.lang.Signature;
|
||||||
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
@ -36,12 +37,34 @@ import java.util.List;
|
|||||||
public class ApiParamsAspect {
|
public class ApiParamsAspect {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ApiParamsAspect.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ApiParamsAspect.class);
|
||||||
|
|
||||||
@Pointcut("execution(public * com.cm..*Controller.*(..))")
|
@Pointcut("execution(public * com.cm.*.controller..*.*(..))")
|
||||||
public void controllerCutPoint() {
|
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 {
|
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();
|
Signature signature = joinPoint.getSignature();
|
||||||
Object[] args = joinPoint.getArgs();
|
Object[] args = joinPoint.getArgs();
|
||||||
Class<?> targetClazz = joinPoint.getTarget().getClass();
|
Class<?> targetClazz = joinPoint.getTarget().getClass();
|
||||||
|
@ -7,7 +7,7 @@ package com.cm.common.exception.base;
|
|||||||
* @Date: 2019/3/4 6:04 PM
|
* @Date: 2019/3/4 6:04 PM
|
||||||
* @Version: 1.0
|
* @Version: 1.0
|
||||||
**/
|
**/
|
||||||
public class SystemException extends Exception {
|
public class SystemException extends RuntimeException {
|
||||||
|
|
||||||
private boolean withMsg = false;
|
private boolean withMsg = false;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user