这种方式简单点 但是可配置性不高
注意:一定得扫描到spring容器中
创建一个类 实现 filter接口
init:该方法是对filter对象进行初始化的方法,仅在容器初始化filter对象结束后被调用一次,参数FilterConfig可以获得filter的初始化参数;
doFilter:可以对request和response进行88f336217b3880082bb52d49b5de60a5预处理04e11139013d3ca73accef66348e2e58
。其中FilterChain可以将处理后的
request和response对象传递到过滤链上的下一个资源。
destroy():该方法在容器销毁对象前被调用。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @Component public class LogFilter implements Filter { private static final Logger LOG = LoggerFactory.getLogger(LogFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { // 打印请求信息 HttpServletRequest request = (HttpServletRequest) servletRequest; LOG.info("------------- LogFilter 开始 -------------"); LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod()); LOG.info("远程地址: {}", request.getRemoteAddr()); long startTime = System.currentTimeMillis(); filterChain.doFilter(servletRequest, servletResponse); LOG.info("------------- LogFilter 结束 耗时:{} ms -------------", System.currentTimeMillis() - startTime); } }
结果
总结
1.过滤器用来实现通用的功能,减少代码冗余,提高可维护性;
2.一个过滤器可以配置给多个资源使用(编码过滤器);
3.一个资源也可以配置多个过滤器,按照配置顺序调用。
如果不懂 请先看了 介绍再来
拦截器的介绍
话不说多 直接上代码
创建拦截器
/** * 拦截器:Spring框架特有的,常用于登录校验,权限校验,请求日志打印 /login * @author : look-word * 2022-06-26 13:55 **/ @Component public class LogInterceptor implements HandlerInterceptor { private static final Logger LOG = LoggerFactory.getLogger(LogInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 打印请求信息 LOG.info("------------- LogInterceptor 开始 -------------"); LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod()); LOG.info("远程地址: {}", request.getRemoteAddr()); long startTime = System.currentTimeMillis(); request.setAttribute("requestStartTime", startTime); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { long startTime = (Long) request.getAttribute("requestStartTime"); LOG.info("------------- LogInterceptor 结束 耗时:{} ms -------------", System.currentTimeMillis() - startTime); } }
注册拦截器
把我们的拦截器 注册到 拦截器链中
/** * @author : look-word * 2022-06-26 14:03 **/ @Configuration public class SpringMvcConfig implements WebMvcConfigurer { @Resource private LogInterceptor logInterceptor; /** * 注册拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry .addInterceptor(logInterceptor) .addPathPatterns("/**")// 对那些接口拦截 .excludePathPatterns("/login");// 对哪些接机口放行 WebMvcConfigurer.super.addInterceptors(registry); } }
测试结果
以上是SpringBoot实现过滤器拦截器的耗时实例对比分析的详细内容。更多信息请关注PHP中文网其他相关文章!