spring boot配置拦截器/过滤器,并指定顺序及规则

大家推荐个靠谱的公众号程序员探索之路,公众号内点击网赚获取彩蛋,大家一起加油,这个公众号已经接入图灵
 ​  ​
拦截器:
package com.zzh.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;
import java.time.Instant;

@Component
public class ControllerTimeInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      request.setAttribute("StarTime", Instant.now());
      return true;
   }

   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

   }

   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
      System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
      System.out.println(((HandlerMethod)handler).getMethod().getName());
      System.out.println("耗时-->"+Duration.between((Instant)request.getAttribute("StarTime"), Instant.now()).toMillis());
   }
}
过滤器
package com.zzh.filter;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;

//注解方式
//@Component
//@Order(Integer.MAX_VALUE)//顺序
//@WebFilter(filterName = "ControllerTimeFilter", urlPatterns = {"/api/dept/**"})//过滤器名称,以及过滤url
public class ControllerTimeFilter implements Filter{
   //初始化
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
      System.out.println("ControllerTimeFilter 初始化了");
   }

   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      Instant start = Instant.now();
      chain.doFilter(request, response);
      Instant end = Instant.now();
      System.out.println("ControllerTimeFilter耗时"+ Duration.between(start, end).toMillis());
   }

   @Override
   public void destroy() {
      System.out.println("ControllerTimeFilter 销毁了");
   }
}
package com.zzh.filter;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;

//@Component
//@Order(Integer.MAX_VALUE)//指定过滤器的顺序 值越小越靠前
//@WebFilter(filterName = "ControllerTimeFilter2", urlPatterns = {"/**/list"})
public class ControllerTimeFilter2 implements Filter{
   //初始化
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
      System.out.println("ControllerTimeFilter2 初始化了");
   }

   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      Instant start = Instant.now();
      chain.doFilter(request, response);
      Instant end = Instant.now();
      System.out.println("ControllerTimeFilter2耗时"+ Duration.between(start, end).toMillis());
   }

   @Override
   public void destroy() {
      System.out.println("ControllerTimeFilter 销毁了");
   }
}
MvcConfig
package com.zzh.config;

import com.zzh.filter.ControllerTimeFilter;
import com.zzh.filter.ControllerTimeFilter2;
import com.zzh.interceptor.ControllerTimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{
   @Autowired
   private ControllerTimeInterceptor controllerTimeInterceptor;

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
      //配置 拦截 /list结尾的请求
      //    /*表示只拦截 /这一层目录下的/list   比如 拦截/dept/list  不会拦截/api/dept/list
      //    /** 表示拦截  /这一层目录下的包含子目录的/list 比如拦截 /api/dept/list
      //拦截器的顺序  就是这个配置的顺序
      registry.addInterceptor(controllerTimeInterceptor).addPathPatterns("/**/list");
      //假设这里再配置一个  那么 这个就会第二个执行
   }

   /**
    * 拦截器也可以通过下面的方式配置
    */
   @Bean
   public FilterRegistrationBean filterRegistrationBean(){
      ControllerTimeFilter controllerTimeFilter = new ControllerTimeFilter();
      FilterRegistrationBean registration = new FilterRegistrationBean();
      registration.setFilter(controllerTimeFilter);
      //过滤的路径  可以自己写一个集合 放进去
      registration.addUrlPatterns("/*");
      //过滤器的名字
      registration.setName("controllerTimeFilter");
      registration.setOrder(Integer.MAX_VALUE);
      return registration;
   }

   @Bean
   public FilterRegistrationBean filterRegistrationBean2(){
      ControllerTimeFilter2 controllerTimeFilter = new ControllerTimeFilter2();
      FilterRegistrationBean registration = new FilterRegistrationBean();
      registration.setFilter(controllerTimeFilter);
      //过滤的路径  可以自己写一个集合 放进去
      registration.addUrlPatterns("/*");
      //过滤器的名字
      registration.setName("controllerTimeFilter2");
      registration.setOrder(Integer.MAX_VALUE-1);
      return registration;
   }
}

效果:耗时-->190
ControllerTimeFilter耗时196
ControllerTimeFilter2耗时196

你可能感兴趣的:(spring,boot)