java使用Filter過濾器對Response返回值進行修改
阿新 • • 發佈:2019-04-13
author tel 過去 ack generate rip read ner extends
練習時只做了對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>
java使用Filter過濾器對Response返回值進行修改