springboot如何獲取request請(qǐng)求的原始url與post參數(shù)
獲取請(qǐng)求的完整url與請(qǐng)求參數(shù),post的body中的數(shù)據(jù),最根本的是利用HttpServletRequest ,來獲取信息,然后是可以使用不同的方式,比如aop攔截,spring中已有過濾器類,方便使用
使用springboot的aop攔截
import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @Aspect @Component public class MyAspect { private final static Logger logger = LoggerFactory.getLogger(MyAspect.class); //這個(gè)切點(diǎn)的表達(dá)式需要根據(jù)自己的項(xiàng)目來寫 @Pointcut("execution(public * com..*(..))") public void log() { } @Before("log()") public void doBefore(JoinPoint joinPoint) { logger.info("aop doBefore.."); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); logger.info("url={}",request.getRequestURI()); logger.info("method={}", request.getMethod()); logger.info("ip={}", request.getRemoteAddr()); logger.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); Enumeration<String> paramter = request.getParameterNames(); while (paramter.hasMoreElements()) { String str = (String) paramter.nextElement(); logger.info(str + "={}", request.getParameter(str)); } } @After("log()") public void doAfter() { logger.info("aop doAfter"); } }
使用filter攔截器
@Bean public Filter loggingFilter(){ AbstractRequestLoggingFilter f = new AbstractRequestLoggingFilter() { @Override protected void beforeRequest(HttpServletRequest request, String message) { System.out.println("beforeRequest: " +message); StringBuilder msg = new StringBuilder(); ServletRequest copiedRequest = new HttpServletRequestWrapper(request); byte[] requestBody; try { requestBody = IOUtils.toByteArray(request.getInputStream()); msg.append(new String(requestBody)); } catch (IOException e) { e.printStackTrace(); } System.out.println("msg:"+msg); } @Override protected void afterRequest(HttpServletRequest request, String message) { System.out.println("afterRequest: " +message); } }; f.setIncludeClientInfo(true); f.setIncludePayload(true); // 在afterRequest的message最后會(huì)有post的body f.setIncludeQueryString(true); f.setIncludeHeaders(true); f.setMaxPayloadLength(10000); f.setBeforeMessagePrefix("BEFORE REQUEST [>"); f.setBeforeMessageSuffix("<]\n"); f.setAfterMessagePrefix("AFTER REQUEST [>"); f.setAfterMessageSuffix("<]\n"); return f; }
同類的還有CommonsRequestLoggingFilter
好處:
可以快速自定義攔截哪些url在shouldLog
方法配置
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CommonsRequestLoggingFilter; import javax.servlet.http.HttpServletRequest; @Configuration public class LoggingFilter extends CommonsRequestLoggingFilter { private final Logger logger = LoggerFactory.getLogger(LoggingFilter.class); private static long beforeTime; private static long afterTime; public MyDeskCalendarLoggingFilter(){ super.setIncludeClientInfo(false); super.setIncludeHeaders(false); super.setIncludePayload(true); super.setMaxPayloadLength(2000); super.setIncludeQueryString(true); } @Override protected boolean shouldLog(HttpServletRequest request) { String requestURI = request.getRequestURI(); // Checks if request matches /api/calendarCompletionDate boolean shouldLog = requestURI.matches("^/api\\/calendarCompletionDate$"); if (shouldLog) { long currentUserId = SecurityUtils.getCurrentAccountUserId(); String method = request.getMethod(); super.setBeforeMessagePrefix("Before request [" + method + "," + "currentUserId:" + currentUserId + ","); //Default is just Before request super.setAfterMessagePrefix("After request [" + method + "," + "currentUserId:" + currentUserId + ","); //Default is just After request } return shouldLog; } @Override protected void beforeRequest(HttpServletRequest request, String message) { beforeTime = System.currentTimeMillis(); MemoryLogUtil.logUsed("beforeRequest");//only shows at this moment in time super.beforeRequest(request, message); } @Override protected void afterRequest(HttpServletRequest request, String message) { afterTime = System.currentTimeMillis(); logger.info("afterRequest: Time taken: " + (afterTime-beforeTime) + " in milliseconds"); MemoryLogUtil.logUsed("afterRequest");//only shows at this moment in time super.afterRequest(request, message); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Stream聚合函數(shù)如何對(duì)BigDecimal求和
這篇文章主要介紹了利用Stream聚合函數(shù)如何對(duì)BigDecimal求和問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)
這篇文章主要介紹了如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)的相關(guān)資料,OAuth2.0是一種開放的授權(quán)協(xié)議,它允許用戶授權(quán)第三方應(yīng)用訪問其賬戶(或資源),而無需共享其用戶賬戶憑據(jù),需要的朋友可以參考下2023-12-12SpringSecurity6.x多種登錄方式配置小結(jié)
SpringSecurity6.x變了很多寫法,本文就來介紹一下SpringSecurity6.x多種登錄方式配置小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12基于Spring p標(biāo)簽和c標(biāo)簽注入方式
這篇文章主要介紹了Spring p標(biāo)簽和c標(biāo)簽注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Mybatis批量更新數(shù)據(jù)庫錯(cuò)誤問題
這篇文章主要介紹了Mybatis批量更新數(shù)據(jù)庫錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Java實(shí)戰(zhàn)項(xiàng)目 圖書管理系統(tǒng)
這篇文章主要介紹了使用java SSM jsp mysql maven設(shè)計(jì)實(shí)現(xiàn)的精品圖書管理系統(tǒng),是一個(gè)很好的實(shí)例,對(duì)大家的學(xué)習(xí)和工作具有借鑒意義,建議收藏一下2021-09-09