java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > spring Response返回值

spring使用Filter过滤器对Response返回值进行修改的方法

作者:谢小涛

这篇文章主要介绍了spring使用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();
            out.close();
        }
    }
    @Override
    public void init(FilterConfig arg0)
        throws ServletException
    {
    }
    @Override
    public void destroy()
    {
    }
}

启动配置:

 /**
     * 配置过滤器,这里过滤器主要是对返回值做后继处理
     * 
     * @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>

到此这篇关于spring使用Filter过滤器对Response返回值进行修改的文章就介绍到这了,更多相关spring Response返回值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文