取消controller的请求信息打印到日志中

注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 使用该注解后,"不会"将controller的请求信息打印到日志中
 * @author wanlf
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiLogIgnore {

}

切面
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.miaoshaproject.annotation.ApiLogIgnore;

/**
 * 接口请求日志切面
 * @author wanlf
 */
@Component
@Aspect
public class ApiLogAop {
  
	private static Logger logger = LoggerFactory.getLogger(ApiLogAop.class);

	@Pointcut("execution(* com.xxx.xxx.feaute.*.controller..*(..))")
	public void controller() {
	}

	@Before("controller()")
	private void beforeController(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		if (method.isAnnotationPresent(ApiLogIgnore.class)) {
			return;
		}
		if (method.isAnnotationPresent(RequestMapping.class) 
		 || method.isAnnotationPresent(PostMapping.class)
		 || method.isAnnotationPresent(PutMapping.class)	
		 || method.isAnnotationPresent(DeleteMapping.class)
	     || method.isAnnotationPresent(GetMapping.class)) {
			StringBuffer sb = new StringBuffer();
			ServletRequestAttributes sra = (ServletRequestAttributes) 
			(RequestContextHolder.getRequestAttributes());
			HttpServletRequest request = sra.getRequest();
			String requestStr = requestToString(request);
			String bodyStr = bodyToString(joinPoint);
			sb.append(requestStr).append(bodyStr);
			logger.info("[接口请求AOP]{}", sb.toString());
		}
	}
	
	/**
	 * 将请求中的body转换为字符串
	 * @param joinPoint
	 * @return
	 */
	private String bodyToString(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		Annotation[][] parameterAnnotations = method.getParameterAnnotations();
		for (Annotation[] annotations : parameterAnnotations) {
			for (Annotation annotation : annotations) {
			if (annotation.annotationType().isAssignableFrom(RequestBody.class)) {
				StringBuffer sb = new StringBuffer();
				Object[] args = joinPoint.getArgs();
				for (Object arg : args) {
					sb.append(arg);
				}
				return sb.toString();
				}
			}
		}
		return "BODY:";
	}
	
	/**
	 * 将请求转换为字符串、除了body
	 * @param request
	 * @return
	 */
	private String requestToString(HttpServletRequest request){
		StringBuffer sb=new StringBuffer();
		
		//请求客户端IP
		
		String ip=IPUtils.getIP(request);
		sb.append("IP:").append(ip).append("|");
		
		//请求URL
		String method=request.getMethod();
		StringBuffer requestURL=request.getRequestURL();
		sb.append(method).append(" ").append(requestURL).append(" |");
		// 请求头
		/*sb.append("HEADERS:[");
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			String headerValue = request.getHeader(headerName);
			sb.append(headerName).append(":").append(headerValue).append(",");
		}
		sb.append("]|");*/
		
		//请求查询参数
		String queryString=request.getQueryString();
		sb.append("QUERY_STRING:").append(queryString).append("|");
		
		//请求表单参数
		sb.append("PARAMETERS:[");
					
		Map<String, String[]> paramterMap = request.getParameterMap();
		for (Map.Entry<String, String[]> key : paramterMap.entrySet()) {
			String[] values = paramterMap.get(key);
			if (values != null && values.length != 0) {
				StringBuffer valueBuffer = new StringBuffer();
				for (String value : values) {
					valueBuffer.append(value).append(",");
				}
				sb.append(key).append(":").append(valueBuffer);
			}
		}
		sb.append("]|");
		return sb.toString();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值