All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.mvc.aop.SysLogAspect Maven / Gradle / Ivy

The newest version!

package com.base4j.mvc.aop;

import com.alibaba.druid.util.StringUtils;
import com.base4j.mvc.annotation.AutoLog;
import com.base4j.mvc.sys.entity.SysLog;
import com.base4j.mvc.sys.service.SysLogService;
import com.base4j.mvc.util.JsonUtils;
import com.base4j.mvc.util.LoginUserTool;
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.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.Date;


@Aspect
@Component
public class SysLogAspect {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private LoginUserTool principalUtil;

    @Pointcut("@annotation(com.base4j.mvc.annotation.AutoLog)")
    public void logPointCut() {

    }

    @Before("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLog sysLog = new SysLog();
     /*   sysLog.setOpeTime(new Date());
        sysLog.setOpeId(principalUtil.getLoginUser().getId());*/

//		解析注解
        AutoLog autoLog = method.getAnnotation(AutoLog.class);
        if (autoLog != null) {
            //注解上的描述
            sysLog.setOperation(autoLog.value());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");
        //请求的参数
        Object[] args = joinPoint.getArgs();
        String params = JsonUtils.toJSONString(args[0]);
        sysLog.setParams(params);
        //获取request
        HttpServletRequest request = getHttpServletRequest();
        //设置IP地址
        sysLog.setIp(getIpAddr(request));

        //保存系统日志
      /*  sysLogService.insert(sysLog);*/
    }

    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            logger.error("occurred while get user's ip address ", e);
        }

        //使用代理,则获取第一个IP地址
//        if(StringUtils.isEmpty(ip) && ip.length() > 15) {
//			if(ip.indexOf(",") > 0) {
//				ip = ip.substring(0, ip.indexOf(","));
//			}
//		}

        return ip;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy