Solution: Spring boot system request / response parameters code for printing _

Key words:

Request parameters, response parameters, filters, interceptor, log filter configured to filter

First, the purpose:

Request and response parameters of system parameters, print output. Support form submission and json submit .

Second, the filter and the interceptor

  First, look at the filters and interceptors. Both functions are very similar, but the specific technology, a far cry. Compared to the previous difference between the two, first understand what AOP, AOP is not a specific technology, but a programming ideas. In the process of object-oriented programming, we can easily through inheritance, polymorphism to solve scale up. But for lateral functions, such as, in all open transaction service method, or the like unified logging functions, object-oriented can not be solved. So AOP-- Oriented Programming is actually a complementary target-oriented programming ideas. And we talked about today filters and interceptors are all concrete realization of aspect-oriented programming. The main difference between the two include the following:

  1, Filter is dependent on the Servlet container, belonging Servlet specification part; the interceptor is independent existence, may be used in any case.

  2, is completed by the execution Filter callback Servlet container; the interceptor is typically performed by a dynamic proxy.

  3, Filter life cycle managed by the Servlet container; the interceptors can be managed by IoC container; Thus, other examples can be obtained by injecting Bean, etc., and therefore will be more convenient to use.

Third, the log filter _ Code

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * json请求时,如果body包含header,则剥除
 * 顺便 输出对应url 请求体,响应体,耗时
 */
public class LogFilter extends OncePerRequestFilter {
	
	private static final Logger log = LoggerFactory.getLogger(LogFilter.class);

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		long requestTime = System.currentTimeMillis();
		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = uri.substring(contextPath.length());
		//静态资源 跳过
		if (url.contains(".")) {
			filterChain.doFilter(request, response);
			return;
		}
//		输出请求体
		String requestBody = "";
		String requestContentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

		if (requestContentType != null){
//			xml json
			if (requestContentType.startsWith(MediaType.APPLICATION_JSON_VALUE) || requestContentType.startsWith(MediaType.APPLICATION_XML_VALUE)){
				requestBody = getRequestBody(request);
				final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
				request = new HttpServletRequestWrapper(request) {
					@Override
					public ServletInputStream getInputStream() throws IOException {
						return new ByteArrayServletInputStream(byteArrayInputStream);
					}
				};
//		    普通表单提交
			}else if (requestContentType.startsWith(MediaType.APPLICATION_FORM_URLENCODED_VALUE)){
				requestBody = toJson(request.getParameterMap());
//			文件表单提交
			}else if (requestContentType.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)){
				requestBody = getFormParam(request);
			}
		}

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		response = new HttpServletResponseWrapper(response) {
			@Override
			public ServletOutputStream getOutputStream() throws IOException {
				return new TeeServletOutputStream(super.getOutputStream(), byteArrayOutputStream);
			}
		};

		filterChain.doFilter(request, response);

		long costTime = System.currentTimeMillis() - requestTime;
		String responseBody = "";
//		暂定只有json 输出响应体
		String contentType = response.getHeader(HttpHeaders.CONTENT_TYPE);
		if (contentType != null && contentType.startsWith(MediaType.APPLICATION_JSON_VALUE)){
			responseBody = byteArrayOutputStream.toString();
		}

		if (response.getStatus() >= 200 && response.getStatus() < 300) {
            log.info("URL:{}, total time:{} ms, responseCode:{}, requestBody:{}, responseBody:{}", url, costTime, response.getStatus(), requestBody, responseBody);
        }else {
            log.error("URL:{}, total time:{} ms, responseCode:{}, requestBody:{}, responseBody:{}", url, costTime, response.getStatus(), requestBody, responseBody);
        }
	}

	private String getRequestBody(HttpServletRequest request) {
		int contentLength = request.getContentLength();
		if(contentLength <= 0){
			return "";
		}
		try {
			return IOUtils.toString(request.getReader());
		} catch (IOException e) {
			log.error("获取请求体失败", e);
			return "";
		}
	}

	private String getFormParam(HttpServletRequest request) {
        MultipartResolver resolver = new StandardServletMultipartResolver();
        MultipartHttpServletRequest mRequest = resolver.resolveMultipart(request);

		Map<String,Object> param = new HashMap<>();
        Map<String,String[]> parameterMap = mRequest.getParameterMap();
        if (!parameterMap.isEmpty()){
            param.putAll(parameterMap);
        }
        Map<String, MultipartFile> fileMap = mRequest.getFileMap();
        if(!fileMap.isEmpty()){
            for (Map.Entry<String, MultipartFile> fileEntry : fileMap.entrySet()) {
                MultipartFile file = fileEntry.getValue();
                param.put(fileEntry.getKey(), file.getOriginalFilename()+ "(" + file.getSize()+" byte)");
            }
        }
		return toJson(param);
	}

    private static String toJson(Object object){
		return JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
	}
}

Please note that the code: the FilterChain.doFilter (Request, the Response);

That method is performed prior to recording a timestamp, and a request parameter; then, completion of execution of the request by a filter chain; After the collected response parameters, returns the execution result between the calculated printing.

Four, arranged filter _ Code

import com.myfutech.common.spring.filter.LogFilter;
import com.myfutech.common.spring.filter.UserInfoFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

	@Bean
	public FilterRegistrationBean logFilter() {
		final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		final LogFilter logFilter = new LogFilter();
		filterRegistrationBean.setFilter(logFilter);
		return filterRegistrationBean;
	}

}

reference:

https://www.cnblogs.com/paddix/p/8365558.html

 

Guess you like

Origin blog.csdn.net/jiahao1186/article/details/91870776