import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.glodon.bim5d.controller.BaseController; import com.glodon.bim5d.util.LoggerMsgUtil; @Aspect @Component public class LogAspect { @Pointcut("execution(public void xxx.xxx.xxx.controller.QRCodeController.download(..))") public void pointCut() { } @Before("pointCut()") public void before(JoinPoint joinPoint) throws Exception { //得到代理類 BaseController controller = (BaseController)joinPoint.getTarget(); //得到request HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); String projectId = getProjectId(joinPoint); try { HashMap<String, Object> inputParam = new HashMap<String, Object>(); inputParam.put("uri", uri); inputParam.put("projectId", projectId); inputParam.put("RequestBody", queryString); LoggerMsgUtil.enterMethodInfo(controller.getLogger(), inputParam); } catch (Exception e) { LoggerMsgUtil.Info(controller.getLogger(), "記錄入口參數失敗:{0}", e.getMessage()); } } /** * 從參數列表中獲取projectId * */ private String getProjectId(JoinPoint joinPoint){ String projectId = null; try { //獲取參數名稱和值 Map<String,Object> nameAndArgs = getFieldsNameValueMap(joinPoint); projectId = nameAndArgs.get("projectId") == null ? null : String.valueOf(nameAndArgs.get("projectId")); } catch (Exception e) { } return projectId; } private Map<String,Object> getFieldsNameValueMap(JoinPoint joinPoint) throws Exception { Object[] args = joinPoint.getArgs(); String classType = joinPoint.getTarget().getClass().getName(); Class<?> clazz = Class.forName(classType); String clazzName = clazz.getName(); String methodName = joinPoint.getSignature().getName(); //獲取方法名稱 Map<String,Object > map=new HashMap<String,Object>(); ClassPool pool = ClassPool.getDefault(); ClassClassPath classPath = new ClassClassPath(this.getClass()); 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) { throw new RuntimeException(); } 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; } }