springboot如何获取request请求的原始url与post参数
更新时间:2023年12月28日 10:08:08 作者:qianggetaba
这篇文章主要介绍了springboot如何获取request请求的原始url与post参数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
获取请求的完整url与请求参数,post的body中的数据,最根本的是利用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); //这个切点的表达式需要根据自己的项目来写 @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最后会有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); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
解决@springboottest注解无法加载src/main/resources目录下文件
这篇文章主要介绍了解决@springboottest注解无法加载src/main/resources目录下文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01Java语言实现简单FTP软件 FTP软件效果图预览之上传功能(3)
这篇文章主要为大家详细介绍了Java语言实现简单FTP软件,FTP软件效果图预览之上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-03-03SpringBoot2使用Jetty容器操作(替换默认Tomcat)
这篇文章主要介绍了SpringBoot2使用Jetty容器操作(替换默认Tomcat),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-10-10深入浅析Java中Static Class及静态内部类和非静态内部类的不同
上次有朋友问我,java中的类可以是static吗?我给他肯定的回答是可以的,在java中我们可以有静态实例变量、静态方法、静态块。当然类也可以是静态的,下面小编整理了些关于java中的static class相关资料分享在脚本之家平台供大家参考2015-11-11使用注解+RequestBodyAdvice实现http请求内容加解密方式
这篇文章主要介绍了使用注解+RequestBodyAdvice实现http请求内容加解密方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
最新评论