转:java使用Filter过滤器对Response返回值进行修改

练习时只做了对request 的处理,这里记录一下,filter 对 response的处理。

原文地址: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();
return buffer.toByteArray();
} 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 = wrapperResponse.getContent();//获取返回值
//判断是否有值
if (content.length > 0)
{ String str = new String(content, "UTF-8");
System.out.println("返回值:" + str);
String ciphertext = null; try
{
//......根据需要处理返回值
}
catch (Exception e)
{
e.printStackTrace();
}
//把返回值输出到客户端
ServletOutputStream out = response.getOutputStream();
out.write(ciphertext.getBytes());
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 下直接在web.xml下添加配置即可

 <!-- 返回值过滤器 -->
<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>

---------------------
作者:西门吹水_
来源:CSDN
原文:https://blog.csdn.net/kokjuis/article/details/77371324
版权声明:本文为博主原创文章,转载请附上博文链接!

上一篇:K-Means聚类算法原理


下一篇:hdu1305Immediate Decodability(字典树)