spring aop输出日志中获取request的post参数

  问题:aop 中使用 request.getInputStream()、request.getReader()获取post参数,因为此时存放post参数的流已经关闭,所以报汇报IO异常

解决方案:

一,通过aop切点,读取controller参数

   logger.info("REQUEST:" + JSONObject.toJSONString(joinPoint.getArgs()));

/**  
 * 
 * @Title:  LogAspect.java   
 
 * @Description:    TODO(用一句话描述该文件做什么)   

 * @date:   2018年7月13日 下午3:03:16   
 * @version V1.0 

 *
 */ 
package com.urule.server.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.workflow.commons.framework.CommonFrmService;

/**    
 *
 * @ClassName:  LogAspect     
 * @version V1.0 
 
 * @date: 2018年7月13日  
 * @Description: TODO(aop 输出请求相应) 

 *
 */
@Aspect
@Component
public class LogAspect extends CommonFrmService{
	@Pointcut("execution(public * com.urule.server.controller..*.*(..))")
    public void webLog(){
		
       // this.
    }
@Before("webLog()")
public void doBefore(JoinPoint joinPoint){
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    // 记录下请求内容
    logger.info("URL : " + request.getRequestURL().toString());
    logger.info("HTTP_METHOD : " + request.getMethod());
    logger.info("REQUEST:" + JSONObject.toJSONString(joinPoint.getArgs()));
   }

@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
    // 处理完请求,返回内容
    logger.info("RESPONSE : " + ret);
}
}

二,通过过滤器复制流,实现post数据流的读取

https://blog.csdn.net/Lan_Xuan/article/details/73826065


你可能感兴趣的:(spring)