關於springmvc時request的getReader()和getInputStream()只能調用一次的解決辦法

  最近準備在原有的SSM項目的基礎上添加完善的日誌分析,因爲是APP的後臺系統,以前在規劃APP的時候,並無在APP上作埋點的處理,而若是想要進行埋點處理的話,對於未能新升級的APP用戶來講,就是去了意義,由於只要用戶不升級,埋點就不能在他的APP中運行。因此,就考慮到了在後臺的入口增長日誌的監控。java

  想法老是簡單,可是在實際實現的過程當中卻仍是遇到了問題。因爲APP基本都採用公參的加密校驗,而後採用POST請求傳遞JSON數據。對於通常的請求分析,好比每一個時間段的訪問量,或者每一個方法每一個某塊的統計都簡單,只要在攔截器中新增一個將數據扔到消息隊列中,而後在消費端在進行日誌的分析和處理便可。而後若是要針對每一個用戶在什麼時間段,作了什麼處理,問題就來了,由於這個時候就必須拿到post中的json參數。spring

  有些同窗就說,這不是很簡單麼,直接request.getParameter()不就能夠了嗎?NO,post的json數據是經過流的方式傳遞的,並不能夠直接讀取。OK,那咱們用request.getReader()拿到流而後轉成字符串不就能夠了麼?那麼問題來了,流是隻能流一遍的,一旦讀過了就不會再有了,具體的方法中就拿不到了。說到這裏,其實訪問根本就不會再進到方法體了,由於springmvc的DispatcherServlet就會拋出異常  getReader() has already been called for。。。。。。json

  說了這麼多,下面是重點啦,其實很簡單,就是在過濾器處理request(若是沒有過濾器,那就新增一個便可)數組

  實現方法:先將RequestBody保存爲一個byte數組,而後經過Servlet自帶的HttpServletRequestWrapper類覆蓋getReader()和getInputStream()方法,使流從保存的byte數組讀取。而後再Filter中將ServletRequest替換爲ServletRequestWrapper。代碼以下:

BodyReaderHttpServletRequestWrapper類包裝ServletRequest,將流保存爲byte[],而後將getReader()和getInputStream()方法的流的讀取指向byte[]mvc

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class AuthFilter implements Filter{
    
    public void destroy() {
        
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;  
        if(request instanceof HttpServletRequest) {  
            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request);  
        }
        if(null == requestWrapper) {  
            chain.doFilter(request, response);  
        } else {  
            chain.doFilter(requestWrapper, response);  
        }
    }
    
    /**
     * 初始化函數時,須要獲取排除在外的url
     */
    public void init(FilterConfig config) throws ServletException {
    
    }
}

引用的類app

 1 import java.io.BufferedReader;
 2 import java.io.ByteArrayInputStream;
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 
 6 import javax.servlet.ServletInputStream;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletRequestWrapper;
 9 
10 import jodd.JoddDefault;
11 import jodd.io.StreamUtil;
12 
13 public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
14     
15     private final byte[] body;  
16     
17     public BodyReaderHttpServletRequestWrapper(HttpServletRequest request)throws IOException {  
18         super(request);  
19         body = StreamUtil.readBytes(request.getReader(), JoddDefault.encoding);  
20     }  
21   
22     @Override  
23     public BufferedReader getReader() throws IOException {  
24         return new BufferedReader(new InputStreamReader(getInputStream()));  
25     }  
26   
27     @Override  
28     public ServletInputStream getInputStream() throws IOException {  
29         final ByteArrayInputStream bais = new ByteArrayInputStream(body);  
30         return new ServletInputStream() {  
31   
32             @Override  
33             public int read() throws IOException {  
34                 return bais.read();  
35             }  
36         };  
37     }  
38 }
相關文章
相關標籤/搜索