springboot aop记录用户请求URL和参数

AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待

1.首先在项目pom.xml中引入依赖


   org.springframework.boot
   spring-boot-starter-aop

2.创建切面类MyAspect.java

package com.hll.hlladmin.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author hll
 */
@Aspect
@Component
public class MyAspect {
	private final static Logger logger = LoggerFactory.getLogger(MyAspect.class);

	//这个切点的表达式需要根据自己的项目来写
	@Pointcut("execution(public * com.hll.hlladmin.controller..*(..))")
	public void log() {
		
	}
	
	@Before("log()")
	public void doBefore(JoinPoint joinPoint) {
		logger.info("aop doBefore..");
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
        //url
		logger.info("url={}",request.getRequestURI());
		
		//method
		logger.info("method={}", request.getMethod());
		
		//ip
		logger.info("ip={}", request.getRemoteAddr());
		
		//类方法
		logger.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
		
		//参数
		Enumeration<String> paramter = request.getParameterNames();
		while (paramter.hasMoreElements()) {
			String str = (String) paramter.nextElement();
			logger.info(str + "={}", request.getParameter(str));
		}
		
	}
	
	@After("log()")
	public void doAfter() {
		logger.info("aop doAfter");
	}
}

3.测试
通过Postman请求

http://192.168.0.128:8080/hll-admin/hlladmin/test/test?id=1111&name=hll&createTime=2018-07-17 16:20:33

eclipse控制台打印如下

2018-07-17 16:45:33.086  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : aop doBefore..
2018-07-17 16:45:33.086  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : url=/hll-admin/hlladmin/test/test
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : method=GET
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : ip=192.168.0.128
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : classMethod=com.hll.hlladmin.controller.TestController.test
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : id=1111
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : name=hll
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : createTime=2018-07-17 16:20:33
2018-07-17 16:45:33.087  INFO 5536 --- [nio-8080-exec-1] com.hll.hlladmin.utils.HttpAspect        : aop doAfter..

你可能感兴趣的:(【springboot】)