注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@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;
@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());
}
}
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:";
}
private String requestToString(HttpServletRequest request){
StringBuffer sb=new StringBuffer();
String ip=IPUtils.getIP(request);
sb.append("IP:").append(ip).append("|");
String method=request.getMethod();
StringBuffer requestURL=request.getRequestURL();
sb.append(method).append(" ").append(requestURL).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();
}
}