| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package cn.com.goldenwater.dcproj.target;
- import cn.com.goldenwater.core.web.BaseResponse;
- import cn.com.goldenwater.dcproj.constValue.CommonLabel;
- import cn.com.goldenwater.dcproj.model.BisInspLog;
- import cn.com.goldenwater.id.util.UuidUtil;
- import cn.com.goldenwater.util.common.IPUtils;
- import com.alibaba.fastjson.JSON;
- import io.swagger.annotations.ApiOperation;
- import net.sf.json.JSONObject;
- import org.apache.commons.lang3.StringUtils;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.servlet.http.HttpServletRequest;
- import java.lang.reflect.Method;
- import java.util.*;
- @Aspect
- @Component("logAspect")
- public class LogAspect {
- private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
- @Autowired
- private RedisTemplate redisTemplate;
- @Pointcut("execution(* cn.com.goldenwater.dcproj.controller..*(..)) && !execution(* cn.com.goldenwater.dcproj.controller.index..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.sso..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.ducha.MwrDataDownLoadController.*(..))&& !execution(* cn.com.goldenwater.dcproj.task..*(..))\"")
- public void logPointCut() {
- }
- /**
- * 前置通知 用于拦截操作,在方法返回后执行
- *
- * @param joinPoint 切点
- */
- @AfterReturning(pointcut = "logPointCut()", returning = "returnValue")
- public void doBefore(JoinPoint joinPoint, Object returnValue) {
- handleLog(joinPoint, null, returnValue);
- }
- /**
- * 拦截异常操作,有异常时执行
- *
- * @param joinPoint
- * @param e
- */
- @AfterThrowing(value = "logPointCut()", throwing = "e")
- public void doAfter(JoinPoint joinPoint, Exception e) {
- handleLog(joinPoint, e, null);
- }
- private void handleLog(JoinPoint joinPoint, Exception e, Object returnValue) {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- String persId = request.getHeader("persId");
- String orgId = request.getHeader("orgId");
- String source = request.getHeader("source");
- String uid = request.getHeader("uid");
- try {
- if (StringUtils.isBlank(persId)) {
- persId = "-1";
- }
- // 获得注解
- String reqPath = "";
- String method = request.getMethod();
- if (StringUtils.isBlank(reqPath)) {
- reqPath = request.getRequestURL().toString();
- if (reqPath.contains("/pers/position") || reqPath.contains("/grw/getScoreRule")
- || reqPath.contains("getCaptchaImage") || reqPath.contains("qrcode/login")
- || reqPath.contains("swagger") || reqPath.contains("/dc/insp/pblms/getPblmType")
- || reqPath.contains("/sys/privs/getPrivByUserId")
- ) {
- return;
- }
- }
- String content = "";
- if (StringUtils.isBlank(content)) {
- content = getAnnotationDesc(joinPoint);
- }
- Enumeration<String> enumeration = request.getParameterNames();
- Map<String, String> parameterMap = new HashMap<>();
- while (enumeration.hasMoreElements()) {
- String parameter = enumeration.nextElement();
- parameterMap.put(parameter, request.getParameter(parameter));
- }
- Map header = getHeadersInfo(request);
- if (StringUtils.isNotBlank(persId)) {
- BisInspLog bisInspLog = new BisInspLog();
- bisInspLog.setLogId(UuidUtil.uuid());
- bisInspLog.setContent(content);
- bisInspLog.setUserId(persId);
- bisInspLog.setPath(reqPath);
- bisInspLog.setOrgId(orgId);
- bisInspLog.setSource(source);
- bisInspLog.setMethod(method);
- bisInspLog.setHeader(JSON.toJSONString(header));
- bisInspLog.setRequestBody(Arrays.toString(joinPoint.getArgs()));
- if (returnValue instanceof BaseResponse) {
- BaseResponse baseResponse = (BaseResponse) returnValue;
- bisInspLog.setResponseBody(JSON.toJSONString(baseResponse));
- }
- bisInspLog.setIpAddr(IPUtils.getIpAddr(request));
- bisInspLog.setParams(JSON.toJSONString(parameterMap));
- bisInspLog.setAppModel(uid);
- bisInspLog.setIntm(new Date());
- JSONObject jsonObject = JSONObject.fromObject(bisInspLog);
- redisTemplate.convertAndSend(CommonLabel.channel, jsonObject);
- }
- } catch (Exception exp) {
- log.error("日志记录异常:{}", exp.getMessage(), exp);
- }
- }
- private static String getAnnotationDesc(JoinPoint joinPoint) {
- Signature signature = joinPoint.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
- if (method != null) {
- ApiOperation rm = method.getAnnotation(ApiOperation.class);
- if (rm != null) {
- return rm.value();
- }
- }
- return "";
- }
- private Map<String, String> getHeadersInfo(HttpServletRequest request) {
- Map<String, String> map = new HashMap<>(10);
- Enumeration headerNames = request.getHeaderNames();
- while (headerNames.hasMoreElements()) {
- String key = (String) headerNames.nextElement();
- String value = request.getHeader(key);
- map.put(key, value);
- }
- return map;
- }
- }
|