使用Spring AOP记录访问日志

API:
	Class clazz = joinPoint.getTarget().getClass();  //获取目标类对象
	String methodName = joinPoint.getSignature().getName();  //获取切入点的方法名
	RequestMapping anno = clazz/method.getAnnotation(RequestMapping.class); //获取注解对象
	String[] values = anno.Value();  //获取注解的value属性值
	SecurityContext securityContext = SecurityContextHolder.getContext();
	User user = (User)securityContext.getAuthentication().getPrincipal(); //User是spring提供的
LogAop:
private Date visitTime; //访问时间
private Class clazz;   //当前正在访问的class对象
private Method method; //当前正在访问的method对象
@Autowired
private ISysLogService sysLogServiceImpl;	//service层对象,用于将访问日志存储到数据库

@Autowired        //在web.xml中配置一个RequestContextListener监听器,由spring容器来注入数据
private HttpServletRequest request;	

	

//前置通知
@Before("pc()")
public void doBeforeAdvice(JoinPoint jp) throws NoSuchMethodException {
        visitTime = new Date();
        clazz = jp.getTarget().getClass();
        System.out.println(clazz);
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        if (args == null) {
            method = clazz.getDeclaredMethod(methodName);
        } else {
            Class[] classes = new Class[args.length];
            for (int i = 0; i < classes.length; i++) {
                classes[i] = args[i].getClass();
            }
            method = clazz.getDeclaredMethod(methodName, classes);
        }
    }
  //最终通知
  @After("pc()")  
  public void doAfterAdvice() {
        Long executionTime = new Date().getTime() - visitTime.getTime();
        RequestMapping classAnno = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
        String url = null;
        if (clazz != null && method != null && LogAopTwo.class != clazz) {
            if (classAnno != null) {
                String[] classValues = classAnno.value();
                if (classValues != null && classValues.length > 0) {
                    //获取一级目录
                    String classUrl = classValues[0];
                    url += classUrl;
                    RequestMapping methodAnno = (RequestMapping) method.getAnnotation(RequestMapping.class);
                    if (methodAnno != null) {
                        String[] methodValues = methodAnno.value();
                        if (methodValues != null && methodValues.length > 0) {
                            //获取二级目录
                            String methodUrl = methodValues[0];
                            //获得请求URL
                            url += methodUrl;
                            //获得客户机IP地址
                            String ip = request.getRemoteAddr();
                            //获取username
		SecurityContext securityContext = SecurityContextHolder.getContext();
   		 User user = (User) securityContext.getAuthentication().getPrincipal();
                            String username = user.getUsername();
                            //封装SysLog
                            SysLog sysLog = new SysLog();
                            sysLog.setUsername(username);
                            sysLog.setUrl(url);
                            sysLog.setIp(ip);
                            sysLog.setExecutionTime(executionTime);
                            sysLog.setVisitTime(visitTime);
  sysLog.setMethod("[class:] " + clazz.getName() + " [method:] " + method.getName());
                            sysLogServiceImpl.save(sysLog);
                        }
                    }
                }
            }
        }

    }
SysLog实体类:
    private String id;
    private Date visitTime;
    private String visitTimeStr;
    private String username;
    private String ip;
    private String url;
    private Long executionTime;
    private String method;

猜你喜欢

转载自blog.csdn.net/qq_42514129/article/details/83343210
今日推荐