通过自定义filter,RequestWrapper,ResponseWrapper 处理请求和响应数据,比如修改请求体和响应体的字符编码
1.request 和 response 中的数据都是 存在流中的(缓存中)获取一次就没有了,需要重新写回去。所以需要两个包装类分别继承HttpServletRequestWrapper 和 HttpServletResponseWrapper 对 request和response进行包装,从中获取数据。
package com.hoau.monitor.servlet;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
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 {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
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());
ServletInputStream servletInputStream = new ServletInputStream() {
public boolean isFinished() {
return false;
}
public boolean isReady() {
return false;
}
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;
}
}
package com.hoau.monitor.servlet;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
/**
* @param
* @author
* @create 2019-06-10 13:31
* @return
* @description :
*/
public class ResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
private HttpServletResponse response;
private PrintWriter pwrite;
public ResponseWrapper(HttpServletResponse response) {
super(response);
this.response = response;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
// 将数据写到 byte 中
return new MyServletOutputStream(bytes);
}
/**
* 重写父类的 getWriter() 方法,将响应数据缓存在 PrintWriter 中
*/
@Override
public PrintWriter getWriter() throws IOException {
try {
pwrite = new PrintWriter(new OutputStreamWriter(bytes, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return pwrite;
}
/**
* 获取缓存在 PrintWriter 中的响应数据
*
* @return
*/
public byte[] getBytes() {
if (null != pwrite) {
pwrite.close();
return bytes.toByteArray();
}
if (null != bytes) {
try {
bytes.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes.toByteArray();
}
class MyServletOutputStream extends ServletOutputStream {
private ByteArrayOutputStream ostream;
public MyServletOutputStream(ByteArrayOutputStream ostream) {
this.ostream = ostream;
}
@Override
public void write(int b) throws IOException {
ostream.write(b); // 将数据写到 stream 中
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener listener) {
}
}
}
自定义Filter,主要重写doFilter方法,在doFilter方法中通过包装类RequestWrapper,ResponseWrapper可以获取或修改请求和响应体内容
package com.hoau.monitor.filter;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.hoau.common.exception.PSBCommonException;
import com.hoau.monitor.entity.MonitorData;
import com.hoau.monitor.facade.IhoauMonitorDataFacade;
import com.hoau.monitor.servlet.RequestWrapper;
import com.hoau.monitor.servlet.ResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@WebFilter(urlPatterns = {"/psb/*"}, filterName = "monitorDataFilter")
public class MonitorDataFilter implements Filter {
@Reference(group = "HoauMonitorFacadeServiceImpl")
@Qualifier("ihoauMonitorDataServiceImpl")
IhoauMonitorDataFacade ihoauMonitorDataServiceImpl;
Log log = LogFactory.getLog(MonitorDataFilter.class);
private MonitorData monitorData;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//此处可设置http请求和响应的字符编码格式
servletRequest.setCharacterEncoding("gbk");
servletResponse.setCharacterEncoding("gbk");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
ResponseWrapper responseWrapper = new ResponseWrapper(httpServletResponse);
filterChain.doFilter(requestWrapper, responseWrapper);
monitorData = new MonitorData();
String params = null;
String paramGet = null;
String paramVar = null;
String val = null;
// String meth = httpServletRequest.getMethod();
// 获取参数的方式实际跟接收方式相关(@RequestBody @RequestParam @PathVariable),多种注解一块用时参数拼接?
// 与请求方式无直接关系(post get put delete)
params = requestWrapper.getBody();
paramGet = JSONObject.toJSONString(httpServletRequest.getParameterMap());
paramVar = JSONObject.toJSONString(httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));
if (!"{}".equals(paramGet)&& !"null".equals(paramGet)) {
params = params + paramGet;
}
if (!"{}".equals(paramVar) && !"null".equals(paramVar)) {
params = params + paramVar;
}
log.info("========1.请求参数:" + params);
monitorData.setInterAddress(httpServletRequest.getRequestURL().toString());
monitorData.setAccAccount("");
monitorData.setAccIP(httpServletRequest.getRemoteAddr());
monitorData.setAccParams(params);
monitorData.setInterType("");
byte[] bytes = responseWrapper.getBytes();
val = new String(bytes, "UTF-8");
monitorData.setRetVal(JSONObject.toJSONString(val));
log.info("========2.返回值:" + val);
//将数据 再写到 response 中
byte[] gbks = val.getBytes("gbk");//这里编码转换操作,这里转换的一定要与接口响应的编码相同
servletResponse.setContentLength(gbks.length);//这一步一定要有,否则字节数组长度不够会造成线程一直等待而阻塞
servletResponse.getOutputStream().write(gbks);
servletResponse.getOutputStream().flush();
servletResponse.getOutputStream().close();
try {
ihoauMonitorDataServiceImpl.addMonitorData(monitorData);
} catch (PSBCommonException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
}
}
除了filter 也可以通过切面(配合注解)获取和修改 ServletRequest , ServletResponse ,及请求和响应数据文章来源:https://www.toymoban.com/news/detail-557755.html
package com.hoau.common.auth;
import java.lang.annotation.*;
/**
* @author :
* @create : 2019-07-25 10:24
* @description :监控注解 应用此注解时,请求的第一个参数必须是 HttpServletRequest
*/
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HoauMonitorAnnotation {
}
package com.hoau.common.auth;
import com.alibaba.fastjson.JSONObject;
import com.hoau.common.entity.MonitorData;
import com.hoau.common.servlet.RequestWrapper;
import com.hoau.common.servlet.ResponseWrapper;
import com.hoau.common.util.HttpUtils;
import com.hoau.common.util.JsonUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
/**
* @author : xilipeng
* @create : 2019-07-25 10:25
* @description :HoauMonitorAnnotation 监控切面
*/
@Aspect
@Order(0)//监控模块优先级必须高于 auth 校验模块,否则安全校验不通过时会是监控通知不执行
@Component
public class MonitorAspect {
@Value("${monitor.url}")
protected String monitorUrl;
@Pointcut("execution(public * com.hoau.*.controller.*.*(..)) && "
+ "@annotation(com.hoau.common.auth.HoauMonitorAnnotation)")
public void monitorPointcut() {
}
@Around("monitorPointcut()")
public Object monitorAdvice(ProceedingJoinPoint pjp) throws IOException {
System.out.println("========> system into MonitorAspect ");
Object result = null;
Object[] args = pjp.getArgs();
// BackObject backObject = new BackObject();
MonitorData monitorData = new MonitorData();
try {
//获取请求中的 HttpServletRequest
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest httpServletRequest = requestAttributes.getRequest();
HttpServletResponse response = requestAttributes.getResponse();
// PrintWriter writer = response.getWriter();
// HttpEncodingAutoConfiguration httpEncodingAutoConfiguration;
// ServletOutputStream outputStream = response.getOutputStream();
// outputStream.
ResponseWrapper responseWrapper = new ResponseWrapper(response);
byte[] bytes =responseWrapper.getBytes();
if (null != httpServletRequest) {
//获取请求参数
RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
String params = requestWrapper.getBody();
String paramGet = JSONObject.toJSONString(httpServletRequest.getParameterMap());
if (!"{}".equals(paramGet) && !"null".equals(paramGet)) {
params += paramGet;
}
//获取接口路径
String uri = httpServletRequest.getRequestURI().toString();
// String meth = httpServletRequest.getMethod();
// 获取参数的方式实际跟接收方式相关(@RequestBody @RequestParam @PathVariable),多种注解一块用时参数拼接?
// 与请求方式无直接关系(post get put delete)
Map<String, Object> pathVariables = (HashMap<String, Object>) httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (null != pathVariables && !pathVariables.isEmpty()) {
String paramVar = JSONObject.toJSONString(pathVariables);
params += paramVar;
for (String key : pathVariables.keySet()) {
String val = "/" + pathVariables.get(key).toString();
uri = uri.replace(val, "");
}
}
monitorData.setInterAddress(uri);
System.out.println("========1.接口地址:" + uri);
monitorData.setAccAccount("");
monitorData.setAccIP(httpServletRequest.getRemoteAddr());
//存入 ES 的时间
OffsetDateTime offsetDateTime = Instant.now().atOffset(ZoneOffset.ofHours(8));//2019-08-22T11:49:53.054+08:00
monitorData.setAccTime(offsetDateTime.toString());
System.out.println("========Instant.now().atOffset(ZoneOffset.ofHours(8)):" + offsetDateTime.toString());
monitorData.setAccParams(params);
System.out.println("========2.请求参数:" + params);
monitorData.setInterType("");
} else if (null != args) {//如果未获取到 HttpServletRequest 就只保存请求参数和结果 和请求时间
//存入 ES 的时间
OffsetDateTime offsetDateTime = Instant.now().atOffset(ZoneOffset.ofHours(8));//2019-08-22T11:49:53.054+08:00
monitorData.setAccTime(offsetDateTime.toString());
monitorData.setAccParams(JsonUtils.toJson(args));
System.out.println("========1.接口地址:请求参数中未获取到 httpServletRequest");
System.out.println("========2.请求参数:" + JsonUtils.toJson(args));
}
result = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
// backObject.setBackStatusCode(RestErrorCodeConstants.STATUS_SYSTEM_ERROR);
// backObject.setBackStatusStr(RestErrorCodeConstants.STATUS_SYSTEM_ERROR_INFO + e.getMessage());
// result = JsonUtils.toJson(backObject);
}
monitorData.setRetVal(JSONObject.toJSONString(result));
System.out.println("========3.返回值:" + JSONObject.toJSONString(result));
try {
HttpUtils.sendPostRequest(monitorUrl, JsonUtils.toJson(monitorData), "form");
} catch (Exception e) {
System.out.println("========监控添加失败:" + e.getMessage());
e.printStackTrace();
}
System.out.println("========> system out MonitorAspect ");
return result;
}
}
使用切面也需要filter文章来源地址https://www.toymoban.com/news/detail-557755.html
package com.hoau.common.filter;
import com.hoau.common.servlet.RequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author :
* @create : 2019-09-01 16:23
* @description :
*/
@Component//在 common 包中需要加上此注解,否则扫描不到
@WebFilter(urlPatterns = {"/*"}, filterName = "requestFilter")
public class RequestFilter implements Filter {
private static Logger log = LoggerFactory.getLogger(RequestFilter.class);
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
// ResponseWrapper responseWrapper = new ResponseWrapper(httpServletResponse);
filterChain.doFilter(requestWrapper, httpServletResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
到了这里,关于Spring/SpringBoot 过滤器修改、获取http 请求request中的参数 和 response返回值,比如修改请求体和响应体的字符编码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!