ZuulFilter
获取请求参数、返回参数
BodyReaderHttpServletRequestWrapper
封装请求参数,request请求流只能读取一次,需要封装,否则用过后在controller层则无法获取到请求参数
package cn.com.yusys.yusp.config;
import org.springframework.util.StreamUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] requestBody = null;
public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
requestBody = StreamUtils.copyToByteArray(request.getInputStream());
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
@Override
public BufferedReader getReader() throws IOException{
return new BufferedReader(new InputStreamReader(getInputStream()));
}
}
LoggerPreFilter
过滤请求信息
package cn.com.yusys.yusp.config;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
@Component
public class LoggerPreFilter extends ZuulFilter {
private Logger logger = LoggerFactory.getLogger(LoggerPreFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
BodyReaderHttpServletRequestWrapper requestWrapper = null;
try {
requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
} catch (IOException e) {
logger.error("LoggerPreFilter解析错误:" + e.getMessage());
return null;
}
String method = requestWrapper.getMethod();
logger.info("【请求method】" + method + "【请求url】" + requestWrapper.getRequestURL());
if ("GET".equals(method)){
Map<String, String[]> parameterMap = requestWrapper.getParameterMap();
ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
Set<String> keySet = parameterMap.keySet();
for (String key : keySet) {
keys.add(key);
String valueTemp = Arrays.toString(parameterMap.get(key));
values.add(valueTemp.substring(1,valueTemp.length() -1));
}
logger.info("【请求参数】 keys:" + keys + " values:" + values );
}else if ("POST".equals(method)){
String paramsFromRequestBody = null;
try {
paramsFromRequestBody = getParamsFromRequestBody(requestWrapper);
} catch (IOException e) {
logger.error("LoggerPreFilter getParamsFromRequestBody 解析错误:" + e.getMessage());
return null;
}
logger.info("【请求参数】" + paramsFromRequestBody.replaceAll(" ",""));
}
return null;
}
private String getParamsFromRequestBody(HttpServletRequest request) throws IOException {
BufferedReader br = null;
StringBuilder listString = new StringBuilder();
try {
br = request.getReader();
String str = "";
while ((str = br.readLine()) != null) {
listString.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
return listString.toString();
}
}
LoggerPostFilter
过滤返回参数
package cn.com.yusys.yusp.config;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
@Component
public class LoggerPostFilter extends ZuulFilter {
private Logger logger = LoggerFactory.getLogger(LoggerPostFilter.class);
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
InputStream responseDataStream = context.getResponseDataStream();
String body = null;
try {
body = StreamUtils.copyToString(responseDataStream, Charset.forName("UTF-8"));
} catch (IOException e) {
return null;
}
if (body != null){
logger.info("【返回参数】" + body.replaceAll(" ","").replaceAll(System.getProperty("line.separator"),""));
context.setResponseDataStream(new ByteArrayInputStream(body.getBytes()));
}
return null;
}
}
控制台输出
|