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

本文介绍了一个使用AOP(面向切面编程)技术来记录HTTP请求日志的方法。通过在Spring框架中定义一个切面,该方法能够在请求处理前后记录请求的详细信息,包括路径、参数、访问者IP地址、响应数据以及请求处理所消耗的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值