服务端在定位错误的时候,有时候要还原现场,这就要把当时的所有入参参数都能记录下来,GET还好说,基本NGINX都会记录。但是POST的请求参数基本不会被记录,这就需要我们通过一些小技巧来记录这些参数,放入日志,这里我们通过自定义拦截器来做这个操作。
1.编写自定义拦截器
我们紧接上一章的工程源码来做
package com.example.firstweb.interceptor;import com.example.firstweb.util.Constants;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;@Component
public class AccessInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {String queryPath = request.getContextPath() + request.getServletPath();StringBuffer alldata=new StringBuffer();alldata.append("http://" + request.getServerName() + ":" + request.getServerPort()+ queryPath);//get请求if (null != request.getQueryString()) {alldata.append("?" + request.getQueryString());alldata.append(" ");}else{alldata.append(" ");}//POST请求Enumeration<String> keys = request.getParameterNames();if (null != keys) {while (keys.hasMoreElements()) {String key = keys.nextElement();String value = request.getParameter(key);alldata.append(key + "=" + value + "&");}}Constants.LOG_ACCESS_INFO.info(alldata.toString());alldata.setLength(0);return true;}}
2.配置自定义拦截器
package com.example.firstweb.config;import com.example.firstweb.interceptor.AccessInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import javax.annotation.Resource;
import java.util.List;@Configuration
public class WebConfig implements WebMvcConfigurer {@Resourceprivate AccessInterceptor accessInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 自定义拦截器,添加拦截路径和排除拦截路径registry.addInterceptor(accessInterceptor).addPathPatterns("/**").excludePathPatterns("/hello"); // 排除某些不需要拦截的请求url(即带有/hello请求不会被拦截)}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {
// 需要配置1:----------- 需要告知系统,这是要被当成静态文件的!
// 第一个方法设置访问路径前缀,第二个方法设置资源路径registry.addResourceHandler("/**").addResourceLocations("classpath:");registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");}}
然后在从welcome的控制器里面增加一个post方法
package com.example.firstweb.controller;import com.example.firstweb.exception.CommException;
import com.example.firstweb.model.po.WelcomePo;
import com.example.firstweb.model.vo.WelcomeVo;
import com.example.firstweb.service.WelcomeService;
import com.example.firstweb.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
@Api(value = "welcome controller", tags = "欢迎界面")
public class Welcome {@Autowiredprivate WelcomeService welcomeService;private static final Logger log = Logger.getLogger(Welcome.class);@GetMapping("/welcomeindex")@ApiOperation("欢迎首页的方法1")public ModelAndView welcomeIndex(){ModelAndView view = new ModelAndView("welcomeindex");WelcomePo wpo= welcomeService.getWelcomInfo();WelcomeVo wvo= new WelcomeVo();BeanUtils.copyProperties(wpo, wvo);view.addObject("welcomedata", wvo);//默认控制台输出日志log.info("default log info ");//输出访问日志Constants.LOG_ACCESS_INFO.info("welcome index accesss");//输出用户阅读日志Constants.LOG_USER_READ.info("first user access log ");return view;}@PostMapping("/welcomeindex2")@ApiOperation("欢迎首页的方法2")public void welcomeIndex2(@ApiParam("定制欢迎词") String test){}
}
启动整个工程代码,然后用postman分别GET访问http://localhost:8088/welcomeindex?name=sss&sho=kow 用post访问http://localhost:8088/welcomeindex2
然后在日志文件access.log,分别找到两条日志
程序的源码可以在这里获得链接: https://pan.baidu.com/s/1v23PyXwB4kvxd79jgLurHw 提取码: mkwc