java--springboot aop记录访问者的信息

public class AopTest {
    
    
 @Around("@annotation(requestMapping)")
    public Object recordCallLog(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
    
    
    	long start = System.currentTimeMillis();

        String params = "";

        Object[] args = pjp.getArgs();
        Object[] arguments = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
    
    
            if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
    
    
                continue;
            }
            arguments[i] = args[i];
        }
        if (arguments != null) {
    
    
            try {
    
    
                params = JSONObject.toJSONString(arguments);
            } catch (Exception e) {
    
    
                params = arguments.toString();
            }
        }

        Signature signature = pjp.getSignature();
        String method = signature.getName();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String path = request.getRequestURL().toString();
        //获取访问者的ip地址
        String ipAddr = getIpAddr(request);
        
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        log.info("-:" + path + params);
        System.err.println("访问者ip:" + ipAddr);
        System.err.println("路径" + path);
        System.err.println("参数" + params);
        Object result = pjp.proceed();
        //响应数据
        String json = "";

        if (null != result) {
    
    
            json = JSONObject.toJSONString(result);
        }
        long usedTime = System.currentTimeMillis() - start;
        log.info("-:" + path + "|耗时:" + usedTime + "毫秒|响应:" + json);
        System.err.println("json:"+json);
        String today = DateUtils.getToday();
        map.put("path", path);//路径
        map.put("content", params);//参数
        map.put("user_ip", ipAddr);//访问者ip
        map.put("time_consuming", usedTime);//耗时
        map.put("response", json);//响应
        map.put("handlers", ipAddr);//操作者
        map.put("addtime", today);//操作时间
        xxxx.add(map);
        return result;
    }


    //获取访问者的ip地址
    public static String getIpAddr(HttpServletRequest request) {
    
    
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    
    
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    
    
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    
    
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
    
    
                try {
    
    
                    ipAddress = InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        if (ipAddress != null && ipAddress.length() > 15) {
    
    
            // = 15
            if (ipAddress.indexOf(",") > 0) {
    
    
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
   }

Guess you like

Origin blog.csdn.net/bpdwg888/article/details/115208385