springboot filter實現(xiàn)請求響應全鏈路攔截
一、為什么你需要這個過濾器??
日志痛點:
- 請求參數(shù)散落在各處?
- 響應數(shù)據(jù)無法統(tǒng)一記錄?
- 日志與業(yè)務代碼嚴重耦合?
??解決方案??: 一個Filter同時攔截請求和響應,實現(xiàn)??日志采集自動化??!
??二、核心實現(xiàn):一個Filter搞定雙向數(shù)據(jù)流??
過濾器設計亮點??
- 請求參數(shù)捕獲??:GET/POST參數(shù)統(tǒng)一解析
- 響應結果截取??:支持JSON/XML等文本響應
- 零代碼侵入??:不修改業(yè)務代碼即可植入監(jiān)控
- ??JDK1.8完美兼容??:無任何新特性依賴
??三、完整代碼實現(xiàn)??
??1. 過濾器核心代碼(可直接復制)??
import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RequestResponseLogFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // 包裝請求響應對象 ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request); ContentCachingResponseWrapper wrappedResponse = new ContentCachingResponseWrapper(response); try { // 執(zhí)行后續(xù)過濾器鏈 filterChain.doFilter(wrappedRequest, wrappedResponse); // 記錄日志(核心邏輯) logRequest(wrappedRequest); logResponse(wrappedResponse); } finally { // 必須執(zhí)行響應回寫 wrappedResponse.copyBodyToResponse(); } } // 請求日志方法 private void logRequest(ContentCachingRequestWrapper request) { String method = request.getMethod(); String url = request.getRequestURL().toString(); String params = getRequestParams(request); System.out.printf("[請求] %s %s | 參數(shù)=%s%n", method, url, params); } // 響應日志方法 private void logResponse(ContentCachingResponseWrapper response) throws IOException { int status = response.getStatus(); String body = getResponseBody(response); System.out.printf("[響應] 狀態(tài)碼=%d | 內容=%s%n", status, body); } // 獲取請求參數(shù)工具方法 private String getRequestParams(ContentCachingRequestWrapper request) { try { if ("GET".equalsIgnoreCase(request.getMethod())) { return request.getQueryString(); } else { byte[] body = request.getContentAsByteArray(); return new String(body, request.getCharacterEncoding()); } } catch (Exception e) { return "[參數(shù)解析失敗]"; } } // 獲取響應內容工具方法 private String getResponseBody(ContentCachingResponseWrapper response) throws IOException { byte[] content = response.getContentAsByteArray(); return new String(content, response.getCharacterEncoding()); } @Override public void init(FilterConfig filterConfig) {} @Override public void destroy() {} }
四、過濾器添加三步走??
??1. 添加依賴(Maven配置)??
<!-- 核心包裝類 --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>9.0.65</version> </dependency>
??2. web.xml配置??(傳統(tǒng)項目)
<filter> <filter-name>RequestResponseLogFilter</filter-name> <filter-class>com.example.RequestResponseLogFilter</filter-class> </filter> <filter-mapping> <filter-name>RequestResponseLogFilter</filter-name> <url-pattern>/*</url-pattern> <!-- 攔截所有請求 --> <dispatcher>REQUEST</dispatcher> </filter-mapping>
????3. Spring Boot集成??(新項目推薦)
@Bean public FilterRegistrationBean<RequestResponseLogFilter> logFilter(){ FilterRegistrationBean<RequestResponseLogFilter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new RequestResponseLogFilter()); registrationBean.addUrlPatterns("/*"); return registrationBean; }
五、運行效果演示??
[REQUEST] POST http://api/user/login | 參數(shù)=username=admin&password=123456 | 時間=Wed Oct 05 14:30:00 CST 2023
[RESPONSE] 狀態(tài)碼=200 | 響應內容={"code":0,"msg":"登錄成功"} | 耗時=120ms
六、必須注意的6大事項??
1.內存溢出風險??
攔截大文件上傳時(>1MB)會占用大量內存
解決方案:限制緩存大小
// 在構造方法中設置最大緩存 new ContentCachingRequestWrapper(request, 1024 * 1024); // 1MB
2.??編碼兼容性問題??
請求參數(shù)亂碼常因缺少編碼設置導致
強制設置編碼(在Filter頭部添加)
request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
3.??響應流二次讀取??
原始響應流只能讀取一次,必須使用包裝類
錯誤寫法:直接使用原始response.getWriter()
4.敏感信息泄露??
密碼等字段需過濾(正則替換示例)
params.replaceAll("password=\\w+", "password=?**?*");
5.性能損耗控制??
高并發(fā)場景建議異步記錄日志
CompletableFuture.runAsync(() -> log.info(logContent));
6.HTTPS支持??
確保Tomcat配置正確:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" scheme="https" secure="true"/>
七、進階玩法??
組合使用??:
搭配Spring AOP實現(xiàn)方法級日志
結合Redis實現(xiàn)請求限流
數(shù)據(jù)分析??:
-- 日志分析SQL示例 SELECT request_uri, COUNT(*) as total, AVG(response_time) as avg_time FROM access_log WHERE status >= 500 GROUP BY request_uri;
總結??
一個優(yōu)秀的Filter就像「數(shù)字監(jiān)控攝像頭」,默默記錄系統(tǒng)運行軌跡。通過本文的方案,你可以:
- 統(tǒng)一管理所有請求響應日志
- 零成本實現(xiàn)全鏈路追蹤
- 靈活擴展監(jiān)控維度
到此這篇關于springboot filter實現(xiàn)請求響應全鏈路攔截的文章就介紹到這了,更多相關springboot filter請求攔截內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字數(shù)量的方法
這篇文章主要介紹了Java簡單統(tǒng)計字符串中漢字,英文字母及數(shù)字數(shù)量的方法,涉及java針對字符串的遍歷、編碼轉換、判斷等相關操作技巧,需要的朋友可以參考下2017-06-06Java實現(xiàn)線程按序交替執(zhí)行的方法詳解
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)線程按序交替執(zhí)行,文中的示例代碼講解詳細,對我們了解線程有一定幫助,需要的可以參考一下2022-10-10Java并發(fā)讀寫鎖ReentrantReadWriteLock 使用場景
ReentrantReadWriteLock是Java中一種高效的讀寫鎖,適用于讀多寫少的并發(fā)場景,它通過允許多個線程同時讀取,但在寫入時限制為單線程訪問,從而提高了程序的并發(fā)性和性能,本文給大家介紹Java并發(fā)讀寫鎖ReentrantReadWriteLock 使用場景,感興趣的朋友跟隨小編一起看看吧2024-10-10Mybatis中一條SQL使用兩個foreach的問題及解決
這篇文章主要介紹了Mybatis中一條SQL使用兩個foreach的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02