一、背景
在项目开发过程中,需要对于某些接口统一处理。
这时候就需要获取响应的报文,再对获取的报文进行统一处理。
二、了解过滤器
首先了解一下过滤器拦截器的区别:
JAVA中的拦截器、过滤器:https://blog.csdn.net/qq_38254635/article/details/131201519
三、代码
新建 WebResponseWrapper.java
重写 HttpServletResponseWrapper,处理json报文请求
package com.config.filter;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class WebResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer = null;
private ServletOutputStream out = null;
private PrintWriter writer = null;
public WebResponseWrapper(HttpServletResponse response) throws IOException {
super(response);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
writer = new PrintWriter(new OutputStreamWriter(buffer,this.getCharacterEncoding()));
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
@Override
public PrintWriter getWriter() throws UnsupportedEncodingException {
return writer;
}
@Override
public void flushBuffer() throws IOException {
if (out != null) {
out.flush();
}
if (writer != null) {
writer.flush();
}
}
@Override
public void reset() {
buffer.reset();
}
public byte[] getResponseData() throws IOException {
flushBuffer();
return buffer.toByteArray();
}
private class WrapperOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos = null;
public WrapperOutputStream(ByteArrayOutputStream stream)
throws IOException {
bos = stream;
}
@Override
public void write(int b) throws IOException {
bos.write(b);
}
@Override
public void write(byte[] b) throws IOException {
bos.write(b, 0, b.length);
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}
}
新建WebResponseFilter.java,响应过滤器处理参数,加密、字符串替、添加响应参数等等。
package com.config.filter;
import com.Utils;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class WebResponseFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if(checkPath(httpRequest.getRequestURI())){
chain.doFilter(httpRequest, httpResponse);
} else {
response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("UTF-8");
WebResponseWrapper webResponseWrapper = new WebResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, webResponseWrapper);
byte[] resData = webResponseWrapper.getResponseData();
PrintWriter out = response.getWriter();
String encrypt = Utils.handleResponse(new String(resData, "UTF-8"));
out.print(encrypt.replaceAll("[\\s*\t\n\r]", ""));
out.flush();
out.close();
}
}
private boolean checkPath(String url){
if(!"on".equals(ENCRYPT_SWITCH)) return true;
for (String path : RESPONSE_LIST) {
if(url.contains(path)) return true;
}
return false;
}
@Override
public void destroy() {
}
}
说明:
ENCRYPT_SWITCH:开关配置
RESPONSE_LIST:忽略处理路径配置,如一些导出的操作。
Utils.handleResponse():需要对报文进行的处理操作。
相关链接:
SpringBoot过滤器获取请求的参数:https://blog.csdn.net/qq_38254635/article/details/136041159
JAVA中的拦截器、过滤器:https://blog.csdn.net/qq_38254635/article/details/131201519
OK,就这些吧。
有什么不对的还望指正,书写不易,觉得有帮助就点个赞吧!☺☺☺