Skip to main content
 首页 » 编程设计

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

2022年07月18日24lidabo

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

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

原文地址:java使用Filter过滤器对Response返回值进行修改

有时候在开发过程中会有这样一个场景,有时需要对返回的数据进行处理。比如加密啊,添加某些标记啊等等。这种情况我们可以通过过滤器去处理。

过滤器是什么就不介绍了,百度一下,你就知道。

以下是代码:

返回值输出代理类:这个类主要是为了吧Response里面的返回值获取到,因为直接Response没有提供直接拿到返回值的方法。所以要通过代理来取得返回值

复制代码
 1 package com.channel.filter; 
 2   
 3   
 4 import java.io.ByteArrayOutputStream; 
 5 import java.io.IOException; 
 6   
 7 import javax.servlet.ServletOutputStream; 
 8 import javax.servlet.WriteListener; 
 9 import javax.servlet.http.HttpServletResponse; 
10 import javax.servlet.http.HttpServletResponseWrapper; 
11   
12   
13 /** 
14  * 返回值输出代理类 
15  *  
16  * @Title: ResponseWrapper 
17  * @Description: 
18  * @author kokJuis 
19  * @date 上午9:52:11 
20  */ 
21 public class ResponseWrapper extends HttpServletResponseWrapper 
22 { 
23   
24     private ByteArrayOutputStream buffer; 
25   
26     private ServletOutputStream out; 
27   
28     public ResponseWrapper(HttpServletResponse httpServletResponse) 
29     { 
30         super(httpServletResponse); 
31         buffer = new ByteArrayOutputStream(); 
32         out = new WrapperOutputStream(buffer); 
33     } 
34   
35     @Override 
36     public ServletOutputStream getOutputStream() 
37         throws IOException 
38     { 
39         return out; 
40     } 
41   
42     @Override 
43     public void flushBuffer() 
44         throws IOException 
45     { 
46         if (out != null) 
47         { 
48             out.flush(); 
49         } 
50     } 
51   
52     public byte[] getContent() 
53         throws IOException 
54     { 
55         flushBuffer(); 
56         return buffer.toByteArray(); 
57     } 
58   
59     class WrapperOutputStream extends ServletOutputStream 
60     { 
61         private ByteArrayOutputStream bos; 
62   
63         public WrapperOutputStream(ByteArrayOutputStream bos) 
64         { 
65             this.bos = bos; 
66         } 
67   
68         @Override 
69         public void write(int b) 
70             throws IOException 
71         { 
72             bos.write(b); 
73         } 
74   
75         @Override 
76         public boolean isReady() 
77         { 
78   
79             // TODO Auto-generated method stub 
80             return false; 
81   
82         } 
83   
84         @Override 
85         public void setWriteListener(WriteListener arg0) 
86         { 
87   
88             // TODO Auto-generated method stub 
89   
90         } 
91     } 
92   
93 }
复制代码

过滤器类:

复制代码
 1 package com.channel.filter; 
 2   
 3   
 4 import java.io.IOException; 
 5   
 6 import javax.servlet.Filter; 
 7 import javax.servlet.FilterChain; 
 8 import javax.servlet.FilterConfig; 
 9 import javax.servlet.ServletException; 
10 import javax.servlet.ServletOutputStream; 
11 import javax.servlet.ServletRequest; 
12 import javax.servlet.ServletResponse; 
13 import javax.servlet.http.HttpServletResponse; 
14   
15 import com.channel.util.RsaEncryptUtil; 
16   
17   
18 /** 
19  * 返回值输出过滤器,这里用来加密返回值 
20  *  
21  * @Title: ResponseFilter 
22  * @Description: 
23  * @author kokJuis 
24  * @date 上午9:52:42 
25  */ 
26 public class ResponseFilter implements Filter 
27 { 
28   
29     @Override 
30     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 
31         throws IOException, ServletException 
32     { 
33         ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse)response);//转换成代理类 
34         // 这里只拦截返回,直接让请求过去,如果在请求前有处理,可以在这里处理 
35         filterChain.doFilter(request, wrapperResponse); 
36         byte[] content = wrapperResponse.getContent();//获取返回值 
37     //判断是否有值 
38         if (content.length > 0) 
39         { 
40   
41             String str = new String(content, "UTF-8"); 
42             System.out.println("返回值:" + str); 
43             String ciphertext = null; 
44   
45             try 
46             { 
47                 //......根据需要处理返回值 
48             } 
49             catch (Exception e) 
50             { 
51                 e.printStackTrace(); 
52             } 
53         //把返回值输出到客户端 
54             ServletOutputStream out = response.getOutputStream(); 
55             out.write(ciphertext.getBytes()); 
56             out.flush(); 
57         } 
58   
59     } 
60   
61     @Override 
62     public void init(FilterConfig arg0) 
63         throws ServletException 
64     { 
65   
66     } 
67   
68     @Override 
69     public void destroy() 
70     { 
71   
72     } 
73   
74 }
复制代码


配置很简单:

spring boot:

复制代码
 1    /** 
 2      * 配置过滤器,这里过滤器主要是对返回值做后继处理 
 3      *  
 4      * @return 
 5      */ 
 6     @Bean 
 7     public FilterRegistrationBean someFilterRegistration() 
 8     { 
 9         FilterRegistrationBean registration = new FilterRegistrationBean(); 
10         registration.setFilter(new ResponseFilter());// 配置一个返回值加密过滤器 
11         registration.addUrlPatterns("/*"); 
12         registration.addInitParameter("paramName", "paramValue"); 
13         registration.setName("responseFilter"); 
14         return registration; 
15     }
复制代码

spring mvc 下直接在web.xml下添加配置即可

复制代码
 1 <!-- 返回值过滤器 --> 
 2     <filter> 
 3         <filter-name>responseFilter</filter-name> 
 4         <filter-class>com.im.filter.ResponseFilter</filter-class> 
 5     </filter> 
 6   
 7     <filter-mapping> 
 8         <filter-name>responseFilter</filter-name> 
 9         <url-pattern>/*</url-pattern> 
10     </filter-mapping>
复制代码

本文参考链接:https://www.cnblogs.com/maohuidong/p/10702085.html