Created
April 19, 2017 03:30
-
-
Save appleshan/2c5544b47cefc47e7d3378684d2853eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.daan.lis.api.module.api.controller.v2.report; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
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.context.request.RequestAttributes; | |
import org.springframework.web.context.request.RequestContextHolder; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.lang.reflect.Method; | |
import java.util.LinkedHashSet; | |
import java.util.Map; | |
import java.util.Set; | |
/** | |
* 拦截器:记录接口调用日志 | |
* @author apple.shan | |
* Created by boss on 2017/4/18. | |
*/ | |
@Aspect //定义一个切面 | |
@Component | |
public class ControllerInterceptor { | |
private static final Logger logger = LoggerFactory.getLogger(ControllerInterceptor.class); | |
/** | |
* 定义切点 (拦截规则):拦截 controller 包下面的所有类中,有 @RequestMapping 注解的方法。 | |
*/ | |
@Pointcut("execution(* com.daan.lis.api.module.api.controller..*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)") | |
public void controllerMethodPointcut(){} | |
/** | |
* 拦截器具体实现 | |
* @param pjp | |
* @return JsonResult(被拦截方法的执行结果,或需要登录的错误提示。) | |
*/ | |
@Around("controllerMethodPointcut()") //指定拦截器规则;也可以直接把“execution(* com.daan.........)”写进这里 | |
public Object doAround(ProceedingJoinPoint pjp){ | |
logger.info("==================== 获取请求的参数以及其返回值 ===================="); | |
logger.info("==================== 获取请求 url 以及参数 ===================="); | |
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); | |
ServletRequestAttributes sra = (ServletRequestAttributes) ra; | |
HttpServletRequest http_request = sra.getRequest(); | |
String http_url = http_request.getRequestURL().toString(); | |
String http_method = http_request.getMethod(); | |
String http_uri = http_request.getRequestURI(); | |
logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}", | |
http_url, http_method, http_uri); | |
logger.info("==================== 获取 java 类的信息 ===================="); | |
MethodSignature signature = (MethodSignature) pjp.getSignature(); | |
String className = signature.getDeclaringTypeName(); //获取被拦截的java 类 | |
Method method = signature.getMethod(); //获取被拦截的方法 | |
String methodName = method.getName(); //获取被拦截的方法名 | |
logger.info("请求开始,类名{},方法:{}", className, methodName); | |
logger.info("==================== 获取请求参数 ===================="); | |
Set<Object> allParams = new LinkedHashSet<>(); //保存所有请求参数,用于输出到日志中 | |
Object[] args = pjp.getArgs(); | |
for(Object arg : args){ | |
//logger.debug("arg: {}", arg); | |
if (arg instanceof Map<?, ?>) { | |
//提取方法中的MAP参数,用于记录进日志中 | |
@SuppressWarnings("unchecked") | |
Map<String, Object> map = (Map<String, Object>) arg; | |
allParams.add(map); | |
}else if(arg instanceof HttpServletRequest){ | |
HttpServletRequest request = (HttpServletRequest) arg; | |
//获取query string 或 posted form data参数 | |
Map<String, String[]> paramMap = request.getParameterMap(); | |
if(paramMap!=null && paramMap.size()>0){ | |
allParams.add(paramMap); | |
} | |
}else if(arg instanceof HttpServletResponse){ | |
//do nothing... | |
}else{ | |
//allParams.add(arg); | |
} | |
} | |
logger.info("请求参数:{}", allParams); | |
Object result = null; | |
try { | |
result = pjp.proceed(); | |
} catch (Throwable e) { | |
logger.info("exception: ", e); | |
} | |
logger.info("==================== 获取请求的返回值 ===================="); | |
Gson gson = new Gson(); | |
logger.info("请求结束,controller的返回值是 " + gson.toJson(result)); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment