import javax.servlet.http.HttpServletRequest;
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.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by xuweilin on 12/16/16.
*/
@Aspect //[color=red]定义一个切面[/color]
@Configuration
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);
// [color=red]定义切点Pointcut,UserController类的所有方法[/color]
@Pointcut("execution(* com.myhexin.xwl.UserController.*(..))")
public void excudeService() {
}
// [color=red]定义切点Pointcut,controller包和所有子包的所有类的方法执行[/color]
@Pointcut("execution(* com.myhexin.xwl.controller..*.*(..))")
public void excudeService2() {
}
//[color=red]表示上面两个都匹配[/color]
@Pointcut("excudeService() || excudeService2()")
public void excudeService3() {
}
@Around("excudeService3()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String url = request.getRequestURL().toString();
String method = request.getMethod();
String uri = request.getRequestURI();
String queryString = request.getQueryString();
logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
String res=queryString.replaceAll("%22", "");
logger.info(res);
String[] str=res.split("&");
//[color=red]判断传入的参数是否为空[/color]
for(String str1:str){
String[] str2=str1.split("=");
if(str2.length<=1){
logger.info(str2[0]+"为空!");
break;
}else if(StringUtils.isEmpty(str2[1])){
logger.info(str2[0]+"为空!");
break;
}
}
//[color=red]获取将要执行的方法名称 [/color]
String methodName = pjp.getSignature().getName();
//[color=red]获取执行方法的参数[/color]
Object[] args = pjp.getArgs();
args[args.length-1]=true;
//[color=red]从参数列表中获取参数对象[/color]
for(Object obj : args){//[color=red]查看参数值[/color]
System.out.println("***********"+obj.toString());
}
// [color=red]result的值就是被拦截方法的返回值[/color]
Object result = pjp.proceed();
//[color=red]不返回,被拦截的方法返回值不能被接收(如:前台不能接收到值)[/color]
return result;
}
}
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.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by xuweilin on 12/16/16.
*/
@Aspect //[color=red]定义一个切面[/color]
@Configuration
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);
// [color=red]定义切点Pointcut,UserController类的所有方法[/color]
@Pointcut("execution(* com.myhexin.xwl.UserController.*(..))")
public void excudeService() {
}
// [color=red]定义切点Pointcut,controller包和所有子包的所有类的方法执行[/color]
@Pointcut("execution(* com.myhexin.xwl.controller..*.*(..))")
public void excudeService2() {
}
//[color=red]表示上面两个都匹配[/color]
@Pointcut("excudeService() || excudeService2()")
public void excudeService3() {
}
@Around("excudeService3()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String url = request.getRequestURL().toString();
String method = request.getMethod();
String uri = request.getRequestURI();
String queryString = request.getQueryString();
logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
String res=queryString.replaceAll("%22", "");
logger.info(res);
String[] str=res.split("&");
//[color=red]判断传入的参数是否为空[/color]
for(String str1:str){
String[] str2=str1.split("=");
if(str2.length<=1){
logger.info(str2[0]+"为空!");
break;
}else if(StringUtils.isEmpty(str2[1])){
logger.info(str2[0]+"为空!");
break;
}
}
//[color=red]获取将要执行的方法名称 [/color]
String methodName = pjp.getSignature().getName();
//[color=red]获取执行方法的参数[/color]
Object[] args = pjp.getArgs();
args[args.length-1]=true;
//[color=red]从参数列表中获取参数对象[/color]
for(Object obj : args){//[color=red]查看参数值[/color]
System.out.println("***********"+obj.toString());
}
// [color=red]result的值就是被拦截方法的返回值[/color]
Object result = pjp.proceed();
//[color=red]不返回,被拦截的方法返回值不能被接收(如:前台不能接收到值)[/color]
return result;
}
}