java使⽤Filter过滤器对Response返回值进⾏修改
有时候在开发过程中会有这样⼀个场景,有时需要对返回的数据进⾏处理。⽐如加密啊,添加某些标记啊等等。这种情况我们可以通过过滤器去处理。
过滤器是什么就不介绍了,百度⼀下,你就知道。
看看代码吧:
返回值输出代理类:这个类主要是为了吧Response⾥⾯的返回值获取到,因为直接Response没有提供直接拿到返回值的⽅法。所以要通过代理来取得返回值
package com.channel.filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 返回值输出代理类
*
* @Title: ResponseWrapper
* @Description:
* @author kokJuis
* @date 上午9:52:11
*/
public class ResponseWrapper extends HttpServletResponseWrapper
{
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public ResponseWrapper(HttpServletResponse httpServletResponse)
{
super(httpServletResponse);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
}
@Override
public ServletOutputStream getOutputStream()
throws IOException
{
return out;
}
@Override
public void flushBuffer()
throws IOException
{
if (out != null)
{
out.flush();
}
}
public byte[] getContent()
throws IOException
{
flushBuffer();
ByteArray();
}
class WrapperOutputStream extends ServletOutputStream
{
private ByteArrayOutputStream bos;
public WrapperOutputStream(ByteArrayOutputStream bos)
{
this.bos = bos;
}
@Override
public void write(int b)
throws IOException
{
bos.write(b);
}
@Override
public boolean isReady()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void setWriteListener(WriteListener arg0)
{
// TODO Auto-generated method stub
}
}
}
过滤器类:
package com.channel.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import com.channel.util.RsaEncryptUtil;
/**
* 返回值输出过滤器,这⾥⽤来加密返回值
*
* @Title: ResponseFilter
* @Description:
* @author kokJuis
* @date 上午9:52:42
*/
public class ResponseFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException
{
ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse)response);//转换成代理类        // 这⾥只拦截返回,直接让请求过去,如果在请求前有处理,可以在这⾥处理
filterChain.doFilter(request, wrapperResponse);
byte[] content = Content();//获取返回值
//判断是否有值
if (content.length > 0)
{
String str = new String(content, "UTF-8");
System.out.println("返回值:" + str);
String ciphertext = null;
try
{
//......根据需要处理返回值
}
catch (Exception e)
{
e.printStackTrace();
}
/
/把返回值输出到客户端
write的返回值ServletOutputStream out = OutputStream();
out.Bytes());
out.flush();
}
}
@Override
public void init(FilterConfig arg0)
throws ServletException
{
}
@Override
public void destroy()
{
}
}
配置很简单:
spring boot:
/**
* 配置过滤器,这⾥过滤器主要是对返回值做后继处理
*
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration()
{
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ResponseFilter());// 配置⼀个返回值加密过滤器        registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("responseFilter");
return registration;
}
spring mvc 下直接在l下添加配置即可
<!-- 返回值过滤器 -->
<filter>
<filter-name>responseFilter</filter-name>
<filter-class>com.im.filter.ResponseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>responseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。