spring使用Filter過濾器對Response返回值進行修改
阿新 • • 發佈:2019-01-06
返回值輸出代理類:這個類主要是為了吧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>
原文: