Sping記錄Http請(qǐng)求log

有時(shí)候我們要記錄下所有的http請(qǐng)求信息(請(qǐng)求的方法,比如GET;請(qǐng)求的uri,比如/api/v1/users等信息),在spring框架下,很容易實(shí)現(xiàn)這個(gè)需求。

在spring框架下,用戶可以自定義web請(qǐng)求處理前后的攔截器,其中HandlerInterceptor接口是我們可以利用的。這個(gè)接口定義了3個(gè)函數(shù):我們只需要實(shí)現(xiàn)這3個(gè)接口,就可以攔截到所有的web請(qǐng)求信息。

具體做法:

  1. 編寫(xiě)HandlerInterceptor實(shí)現(xiàn)類(lèi),重寫(xiě)其中的方法,這里重寫(xiě)了方法postHandle,這個(gè)調(diào)用是在web請(qǐng)求成功處理后被調(diào)用。
public class PlatformRequestInterceptor implements HandlerInterceptor {
    @Autowired
    private PlatformAccessLogRepository repository;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //這里沒(méi)有直接讀取request對(duì)象,而是通過(guò)`ContentCachingRequestWrapper`類(lèi)先進(jìn)行一次緩存,原因參考參考文檔[1]
        HttpServletRequest requestCacheWrapperObject
                = new ContentCachingRequestWrapper(request);
        requestCacheWrapperObject.getParameterMap();

        String uri = requestCacheWrapperObject.getRequestURI();
        if (!uri.equals("/error")) {
            String method = requestCacheWrapperObject.getMethod();
            String userName = "guest";
            if (requestCacheWrapperObject.getUserPrincipal() != null) {
                userName = requestCacheWrapperObject.getUserPrincipal().getName();
            }
            String remoteAddr = requestCacheWrapperObject.getRemoteAddr();
            PlatformAccessLog accessLog = new PlatformAccessLog();
            accessLog.setMethod(method);
            accessLog.setName(userName);
            accessLog.setPath(uri);
            accessLog.setTime(new Date());
            accessLog.setRemoteAddr(remoteAddr);
            repository.insert(accessLog);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
  1. 注冊(cè)上面寫(xiě)好的攔截器實(shí)現(xiàn),重載WebMvcConfigurerAdapteraddInterceptors方法。
@Configuration
public class PlatformMVCConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private HandlerInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor);
    }
}

經(jīng)過(guò)上面的操作,就可以把所有正常處理的http請(qǐng)求記錄到mongodb中了。寫(xiě)入數(shù)據(jù)庫(kù)的代碼略。

參考文檔

  1. Spring – Log Incoming Requests
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評(píng)論 6 342
  • spring官方文檔:http://docs.spring.io/spring/docs/current/spri...
    牛馬風(fēng)情閱讀 1,843評(píng)論 0 3
  • 大敵當(dāng)前,我經(jīng)常因?yàn)樾募露缟闲褋?lái)~是件好事~問(wèn)題是,當(dāng)房友只需要很少時(shí)間溫習(xí)都比我強(qiáng)~我發(fā)覺(jué)我的腦袋好想爆!而我...
    tonyoppa閱讀 215評(píng)論 0 0
  • 今天寶貝給了我一個(gè)意外驚喜,我在畫(huà)畫(huà),他自己去找來(lái)三角尺和量角器,我以為他要亂畫(huà),因?yàn)樗麤](méi)有用過(guò),甚至都不知道這兩...
    辣媽趙十八閱讀 532評(píng)論 2 2

友情鏈接更多精彩內(nèi)容