Spring基础篇(4)-AOP

JAVA && Spring && SpringBoot2.x — 学习目录

aop前置通知,后置通知,返回通知,环绕通知执行顺序:

进入环绕通知
前置通知
Object result=(boolean)jointPoint.proceed();  //执行该方法
退出环绕通知
后置通知
返回通知

AOP工具类,获取Method对象。

public final class AopUtils {

    private AopUtils() {
        throw new UnsupportedOperationException("It's prohibited to create instances of the class.");
    }

    /**
     * 根据该joinPoint的详细信息
     * Gets a {@link Method} object from target object (not proxy class).
     *
     * @param joinPoint the {@link JoinPoint}
     * @return a {@link Method} object or null if method doesn't exist or if the signature at a join point
     *         isn't sub-type of {@link MethodSignature}
     */
    public static Method getMethodFromTarget(JoinPoint joinPoint) {
        Method method = null;
        if (joinPoint.getSignature() instanceof MethodSignature) {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            method = getDeclaredMethod(joinPoint.getTarget().getClass(), signature.getName(),
                    getParameterTypes(joinPoint));
        }
        return method;
    }

    /**
     * Gets a {@link Method} object from target object by specified method name.
     *
     * @param joinPoint  the {@link JoinPoint}
     * @param methodName the method name
     * @return a {@link Method} object or null if method with specified methodName doesn't exist
     */
    public static Method getMethodFromTarget(JoinPoint joinPoint, String methodName) {
        return getDeclaredMethod(joinPoint.getTarget().getClass(), methodName,
                getParameterTypes(joinPoint));
    }

    /**
     * 获取方法参数的类型
     * Gets parameter types of the join point.
     *
     * @param joinPoint the join point
     * @return the parameter types for the method this object
     *         represents
     */
    public static Class[] getParameterTypes(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        return method.getParameterTypes();
    }

    /**
     * Gets declared method from specified type by mame and parameters types.
     *
     * @param type           the type
     * @param methodName     the name of the method
     * @param parameterTypes the parameter array
     * @return a {@link Method} object or null if method doesn't exist
     */
    public static Method getDeclaredMethod(Class type, String methodName, Class... parameterTypes) {
        Method method = null;
        try {
            method = type.getDeclaredMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return method;
    }


    /**
     * 获取该方法的详细信息
     * @param m
     * @return
     */
    public static String getMethodInfo(Method m) {
        StringBuilder info = new StringBuilder();
        info.append("Method signature:").append("\n");
        info.append(m.toGenericString()).append("\n");

        info.append("Declaring class:\n");
        info.append(m.getDeclaringClass().getCanonicalName()).append("\n");

        info.append("\nFlags:").append("\n");
        info.append("Bridge=").append(m.isBridge()).append("\n");
        info.append("Synthetic=").append(m.isSynthetic()).append("\n");
        info.append("Final=").append(Modifier.isFinal(m.getModifiers())).append("\n");
        info.append("Native=").append(Modifier.isNative(m.getModifiers())).append("\n");
        info.append("Synchronized=").append(Modifier.isSynchronized(m.getModifiers())).append("\n");
        info.append("Abstract=").append(Modifier.isAbstract(m.getModifiers())).append("\n");
        info.append("AccessLevel=").append(getAccessLevel(m.getModifiers())).append("\n");

        info.append("\nReturn Type: \n");
        info.append("ReturnType=").append(m.getReturnType()).append("\n");
        info.append("GenericReturnType=").append(m.getGenericReturnType()).append("\n");

        info.append("\nParameters:");
        Class[] pType = m.getParameterTypes();
        Type[] gpType = m.getGenericParameterTypes();
        if (pType.length != 0) {
            info.append("\n");
        } else {
            info.append("empty\n");
        }
        for (int i = 0; i < pType.length; i++) {
            info.append("parameter [").append(i).append("]:\n");
            info.append("ParameterType=").append(pType[i]).append("\n");
            info.append("GenericParameterType=").append(gpType[i]).append("\n");
        }

        info.append("\nExceptions:");
        Class[] xType = m.getExceptionTypes();
        Type[] gxType = m.getGenericExceptionTypes();
        if (xType.length != 0) {
            info.append("\n");
        } else {
            info.append("empty\n");
        }
        for (int i = 0; i < xType.length; i++) {
            info.append("exception [").append(i).append("]:\n");
            info.append("ExceptionType=").append(xType[i]).append("\n");
            info.append("GenericExceptionType=").append(gxType[i]).append("\n");
        }

        info.append("\nAnnotations:");
        if (m.getAnnotations().length != 0) {
            info.append("\n");
        } else {
            info.append("empty\n");
        }

        for (int i = 0; i < m.getAnnotations().length; i++) {
            info.append("annotation[").append(i).append("]=").append(m.getAnnotations()[i]).append("\n");
        }

        return info.toString();
    }

    private static String getAccessLevel(int modifiers) {
        if (Modifier.isPublic(modifiers)) {
            return "public";
        } else if (Modifier.isProtected(modifiers)) {
            return "protected";
        } else if (Modifier.isPrivate(modifiers)) {
            return "private";
        } else {
            return "default";
        }
    }


    /**
     * 获取切点  类上的特定注解
     * @param joinPoint
     * @param annotation
     * @param 
     * @return
     */
    public static  Optional getAnnotation(JoinPoint joinPoint, Class annotation) {
        return getAnnotation(joinPoint.getTarget().getClass(), annotation);
    }

    /**
     * 获取 类上的特定注解
     * @param type
     * @param annotation
     * @param 
     * @return
     */
    public static  Optional getAnnotation(Class type, Class annotation) {
        Validate.notNull(annotation, "annotation cannot be null");
        Validate.notNull(type, "type cannot be null");
        for (Annotation ann : type.getDeclaredAnnotations()) {
            if (ann.annotationType().equals(annotation)) return Optional.of((T) ann);
        }

        Class superType = type.getSuperclass();
        if (superType != null && !superType.equals(Object.class)) {
            return getAnnotation(superType, annotation);
        }

        return Optional.absent();
    }

}

需求:如何在一个日志切面类中,打印出客户端IP,请求路径,请求参数,返回参数,方法调用时间?

由上面我们可知,我们可以通过环绕通知实现公共参数的打印。

但是需要考虑如下几点:

  1. 如何在环绕通知方法中获取request参数;
  2. 如何获取客户端IP;
  3. 如何获取请求参数;
  4. 如何获取返回结果;

解决方案

  • 获取request对象
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = servletRequestAttributes.getRequest();
  • 获取客户端IP

【客户端】的请求IP到达【负载均衡服务器】(例如Nginx),再转发到【应用服务器】。应用服务器通过request.getRemoteAddr()方法获取的IP时间上是【负载均衡服务器】的IP。一般在Nginx进行配置,故【应用服务器】可以获得request.getHeader("X-Forwarded-For")获取到客户端实际的IP。

为防止篡改IP地址,需要在最外层Nginx配置X-Forwarded-For为$remote_addr拿到客户端的实际IP,防止篡改X-Forwarded-For参数。

服务器如何获取请求IP

  private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotBlank(ip)) {
            if (!"unKnown".equalsIgnoreCase(ip)) {
                String[] ips = ip.split(",");
                return ips[0];
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("Proxy-Client-IP");
        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("WL-Proxy-Client-IP");
        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        //若是没有使用代理,直接获取到服务器请求地址
        String remoteAddr = request.getRemoteAddr();
        return "0:0:0:0:0:0:0:1".equals(remoteAddr) ? "127.0.0.1" : ip;
    }
  • 环绕通知

在spring.xml配置文件中加入:



package com.springmvc.common.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.text.SimpleDateFormat;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * @ClassName SystemLogAspect
 * @Description 日志切面
 * @Author xueruiye
 * @Date 2019/6/6
 * @Version 1.0
 **/
@Component
@Aspect
public class SystemLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

    //环绕通知
    @Around("execution(public  * com.*.web.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object result = null;
        Map printLogParamMap = new HashMap();

        try {
            //获取request对象
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = servletRequestAttributes.getRequest();

            //获取请求的URI地址以及IP地址
            String requestURI = request.getRequestURI();

            //获取完整的url路径()
            //            String requestURL = request.getRequestURL().toString();
            String requestIP = getIp(request);

            //获取切面方法参数
            StringBuilder sb = new StringBuilder();

            if (requestURI.contains("login") ||
                    requestURI.contains("register")) {
                sb.append("[为保护用户隐私,登录时隐藏用户名和密码]");
            } else {
                try {
                    Object[] args = pjp.getArgs();

                    if ((pjp.getArgs() != null) && (pjp.getArgs().length > 0)) {
                        //打印请求参数,但是不打印request、response等参数
                        sb.append("[");

                        int count = 0;

                        for (Object obj : pjp.getArgs()) {
                            count++;

                            String paramType = obj.getClass().getName();

                            if (paramType.contains("HttpServletRequest") ||
                                    paramType.contains("HttpServletResponse") ||
                                    paramType.contains("MyRequestWrapper")) {
                                sb.append(" 参数【").append(count).append("-");
                                sb.append("打印参数类型:").append(paramType)
                                  .append(" ");
                            } else {
                                sb.append(" ").append("参数【" + count + "】—");
                                sb.append("打印参数内容:")
                                  .append(JSONObject.toJSONString(obj))
                                  .append(" ");
                            }
                        }

                        sb.append("]");
                    } else {
                        sb.append("[输入参数为空]");
                    }
                } catch (Exception e) {
                    logger.error("[doAround]获取输入参数失败", e);
                }
            }

            /**
             * 获取拦截方法的返回值-result
             */
            long startTimeMillis = System.currentTimeMillis();
            //获取ResponseVo对象
            result = pjp.proceed();

            long endTimeMillis = System.currentTimeMillis();

            printLogParamMap.put("requestURI", requestURI);
            printLogParamMap.put("clientIP", requestIP);
            printLogParamMap.put("requestParam", sb);
            printLogParamMap.put("responseParam", result);
            printLogParamMap.put("startTimeMillis", startTimeMillis);
            printLogParamMap.put("endTimeMillis", endTimeMillis);
            printLog(printLogParamMap);
        } catch (Exception e) {
            logger.error("操作日志打印失败!", e.getMessage(), e);
        }

        return result;
    }

    /**
     * 日志打印
     * @param printLogParamMap
     */
    private void printLog(Map printLogParamMap) {
        //将毫秒数解析成时间
        Long startTimeMillis = (Long) printLogParamMap.get("startTimeMillis");
        Long endTimeMillis = (Long) printLogParamMap.get("endTimeMillis");
        SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String startTime = sdt.format(startTimeMillis);
        String endTime = sdt.format(endTimeMillis);

        StringBuilder soutParam = new StringBuilder();
        soutParam.append("\n").append("客户端IP:")
                 .append(printLogParamMap.get("clientIP"));
        soutParam.append("\n").append("请求路径:")
                 .append(printLogParamMap.get("requestURI"));
        soutParam.append("\n").append("起止时间:")
                 .append(String.format("%s到%s", startTime, endTime));
        soutParam.append("\n").append("请求耗时:")
                 .append(endTimeMillis - startTimeMillis).append("ms");
        soutParam.append("\n").append("输入参数:")
                 .append(printLogParamMap.get("requestParam"));
        soutParam.append("\n").append("输出参数:")
                 .append(JSON.toJSONString(printLogParamMap.get("responseParam")));
        logger.info(soutParam.toString());
    }

    /**
     * 获取请求Ip
     *
     * @param request
     * @return
     */
    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");

        if (StringUtils.isNotBlank(ip)) {
            if (!"unKnown".equalsIgnoreCase(ip)) {
                String[] ips = ip.split(",");

                return ips[0];
            }
        }

        ip = request.getHeader("X-Real-IP");

        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }

        ip = request.getHeader("Proxy-Client-IP");

        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }

        ip = request.getHeader("WL-Proxy-Client-IP");

        if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }

        //若是没有使用代理,直接获取到服务器请求地址
        String remoteAddr = request.getRemoteAddr();

        return "0:0:0:0:0:0:0:1".equals(remoteAddr) ? "127.0.0.1" : ip;
    }
}

效果图:

客户端IP:127.0.0.1
请求路径:/springmvc/user
起止时间:2019-06-06 16:06:41:466到2019-06-06 16:06:41:498
请求耗时:32ms
输入参数:[输入参数为空]
输出参数:{"empty":false,"model":{"roleEditFlag":"true","roleInfos":[{"id":2,"roleName":"客服"},{"id":93,"roleName":"销售"}]},"modelMap":{"$ref":"$.model"},"reference":true,"viewName":"success"}

你可能感兴趣的:(Spring基础篇(4)-AOP)