1、 增加日志记录表结构
CREATE TABLE `ia_request_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`url` varchar(256) DEFAULT NULL COMMENT '请求地址',
`method` varchar(10) DEFAULT NULL COMMENT '请求方法',
`ip` varchar(32) DEFAULT NULL COMMENT 'ip地址',
`headers` text DEFAULT NULL COMMENT '请求头',
`param` text COMMENT '请求参数',
`body` text COMMENT '请求体body',
`uid` bigint(20) DEFAULT NULL COMMENT '用户编号',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='接口请求记录';
2、 重新封装request,使Body可以重复读取
package com.ven.filter;
import com.ven.util.BodyReaderHttpServletRequestWrapper;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Description: 重新封装request,使Body可以重复读取
* @Author: wenxin
* @Date: 2023/5/25 16:26
* @Version: 1.0
*/
@WebFilter(filterName = "httpServletRequestWrapperFilter", urlPatterns = {"/*"})
@Component
public class HttpServletRequestWrapperFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//初始化表结构
//查询表是否存在
//不存在创建表结构
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ServletRequest requestWrapper = null;
if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
//遇到post方法才对request进行包装
String methodType = httpRequest.getMethod();
if ("POST".equals(methodType)) {
requestWrapper = new BodyReaderHttpServletRequestWrapper(
(HttpServletRequest) request);
}
}
if (null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void destroy() {
}
}
3、 增加日志记录拦截器
package com.ven.interceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.meix.ven.util.BodyReaderHttpServletRequestWrapper;
import com.meix.ven.util.RequestUtil;
import com.meix.ven.util.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Description: 接口请求记录
* @Author: wenxin
* @Date: 2023/5/24 17:55
* @Version: 1.0
*/
@Component
public class RequestLogHandlerInterceptor implements HandlerInterceptor {
private Logger log = LoggerFactory.getLogger(RequestLogHandlerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//请求路径
String url = request.getRequestURI();
//获取请求参数
String paramData = JSON.toJSONString(request.getParameterMap(), SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
//获取headers参数
String headersData = JSON.toJSONString(RequestUtil.getHeaderMap(request), SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
//获取客户端ip
String ip = RequestUtil.getIpAddr(request);
//请求方法
String method = request.getMethod();
String body="";
//获取body参数
if(request instanceof BodyReaderHttpServletRequestWrapper ) {
body = ((BodyReaderHttpServletRequestWrapper) request).getBodyStr();
}
SecurityInfo securityInfo = SecurityUtil.getSecurityInfo();
Long uid = null;
if (securityInfo != null) {
uid = securityInfo.getId();
}
log.info("[RequestLogHandlerInterceptor]调用接口:{},Param:{},Headers:{},请求ip:{},请求方法:{},body:{},用户Id:{}",
url, paramData, headersData, ip, method, body, uid);
return true;
}
}
4、 配置springMVC的拦截器
package com.ven.config;
import com.ven.interceptor.RequestLogHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description:配置mvc
*/
@Configuration
@ComponentScan(basePackages = {"com."})
public class MVCConfig implements WebMvcConfigurer {
@Autowired
private RequestLogHandlerInterceptor requestLogHandlerInterceptor;
/*******************添加拦截器***************/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestLogHandlerInterceptor).addPathPatterns("/**");
}
}