码迷,mamicode.com
首页 > 编程语言 > 详细

springboot添加切面

时间:2019-01-04 00:25:30      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:params   after   注解   nal   getname   表达   catch   try   enum   

package com.bxs.portal2.aspect;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
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.springframework.beans.factory.annotation.Autowired;
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 com.bxs.portal2.entity.SysLogBO;
import com.bxs.portal2.service.SysLogService;
import com.bxs.portal2.util.Serializor;
import com.google.common.collect.Maps;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统日志切面
 * 
 * @author zhuzhe
 * @date 2018/6/4 9:27
 * @email 1529949535@qq.com
 */
@Aspect // 使用@Aspect注解声明一个切面
@Component
@Slf4j
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 这里我们使用注解的形式 当然,我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method 切点表达式:
     * execution(...)
     */
    @Pointcut("execution(* com.bxs.portal2.service..*(..))")
    public void logPointCut() {
    }

    /**
     * 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知)
     * 
     * @param point
     * @return
     * @throws Throwable
     * 
     *             @Before("logPointCut()") public Object
     *             around(ProceedingJoinPoint point) throws Throwable { try {
     *             saveLog(point, 10); } catch (Exception e) { } return null; }
     */

    @Before("logPointCut()")
    public void before(JoinPoint joinPoint) {
        log.info("前置通知");
        // 获取目标方法的参数信息
        Object[] obj = joinPoint.getArgs();
        // AOP代理类的信息
        joinPoint.getThis();
        // 代理的目标对象
        joinPoint.getTarget();
        // 用的最多 通知的签名
        Signature signature = joinPoint.getSignature();
        // 代理的是哪一个方法
        log.info("代理的是哪一个方法" + signature.getName());
        // AOP代理类的名字
        log.info("AOP代理类的名字" + signature.getDeclaringTypeName());
        // AOP代理类的类(class)信息
        signature.getDeclaringType();
        // 获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
        // 如果要获取Session信息的话,可以这样写:
        // HttpSession session = (HttpSession)
        // requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
        // 获取请求参数
        Enumeration<String> enumeration = request.getParameterNames();
        Map<String, String> parameterMap = Maps.newHashMap();
        while (enumeration.hasMoreElements()) {
            String parameter = enumeration.nextElement();
            parameterMap.put(parameter, request.getParameter(parameter));
        }

        Optional<String> str = Serializor.toJson(parameterMap);
        if (obj.length > 0) {
            log.info("请求的参数信息为:" + Serializor.toJson(obj));
        }
    }

    /**
     * 后置最终通知(目标方法只要执行完了就会执行后置通知方法)
     * 
     * @param joinPoint
     */
    @After("logPointCut()")
    public void doAfterAdvice(JoinPoint joinPoint) {
        log.info("后置最终通知执行了!!!!");
    }

    @Around("logPointCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        log.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);

        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
        Gson gson = new Gson();
        log.info("请求结束,controller的返回值是 " + gson.toJson(result));
        return result;
    }

    /**
     * 保存日志
     * 
     * @param joinPoint
     * @param time
     */
    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println(method);
        SysLogBO sysLogBO = new SysLogBO();
        sysLogBO.setExeuTime(time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        sysLogBO.setCreateDate(dateFormat.format(new Date()));
        // 请求的 类名、方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLogBO.setClassName(className);
        sysLogBO.setMethodName(methodName);
        // 请求的参数
        Object[] args = joinPoint.getArgs();
        try {
            List<String> list = new ArrayList<String>();
            for (Object o : args) {
                list.add(new Gson().toJson(o));
            }
            sysLogBO.setParams(list.toString());
        } catch (Exception e) {
        }
        sysLogService.save(sysLogBO);
    }
}

 

springboot添加切面

标签:params   after   注解   nal   getname   表达   catch   try   enum   

原文地址:https://www.cnblogs.com/bxssjava/p/10217596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!