/**建类继承HttpServletRequestWrapper */
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends HttpServletRequestWrapper {
private final String body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
bufferedReader = request.getReader();
if (bufferedReader != null) {
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes("utf-8"));
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody() {
return this.body;
}
}
/**过滤器*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lqting.common.utils.EmptyUtil;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
@WebFilter(filterName = "apiLogFilter", urlPatterns = {"/*"})
public class ApiRequestDataFilter implements Filter {
@Override
public void destroy() {
}
/**
* 从请求中获取数据
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter) throws IOException, ServletException {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String method = httpRequest.getMethod();
//如过请求为Post类型,从Stream中去数据
if ("post".equalsIgnoreCase(method)) {
if (!isMultipartContent(httpRequest)) {
RequestWrapper requestWrapper = new RequestWrapper(httpRequest);
requestWrapper.setAttribute("x-data", requestWrapper.getBody());
filter.doFilter(requestWrapper, response);
} else {
filter.doFilter(httpRequest, response);
}
} else if ("get".equalsIgnoreCase(method)) {
Enumeration<String> pNames = httpRequest.getParameterNames();
if (EmptyUtil.isNotEmpty(pNames)) {
JSONObject data = new JSONObject();
String ele = null;
while (pNames.hasMoreElements()) {
ele = pNames.nextElement();
data.put(ele, httpRequest.getParameter(ele));
}
request.setAttribute("x-data", JSON.toJSONString(data));
}
filter.doFilter(request, response);
} else {
filter.doFilter(request, response);
}
} catch (IOException e) {
e.printStackTrace();
filter.doFilter(request, response);
}
}
/**
* 判断是否是multipart/form-data请求
*
* @param request
* @return
*/
public static boolean isMultipartContent(HttpServletRequest request) {
String contentType = request.getContentType(); //获取Content-Type
if ((contentType != null) && (contentType.toLowerCase().startsWith("multipart/"))) {
return true;
} else {
return false;
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
/**使用*/
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class Test extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(String.valueOf(request.getAttribute("x-data")));
return true;
}
}
/**配置过滤器*/
@SpringBootApplication
@ServletComponentScan
@EnableCaching
public class MainApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MainApplication.class);
}
}