spring boot implements filters and interceptors demo

  • 2020-06-12 09:15:59
  • OfStack

Collate the documentation, find 1 spring boot implementation filter and interceptor demo, slightly collate 1 share.

Interceptor definition:


@WebServlet
public class ActionInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>> Call before the request is processed ( Controller Before the method call) ");

    //  Acquisition of system time 
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    //  Set a limit on the running time  0-4 point 
    if (hour < 4) {
      return true;
    }
    return false;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>> Called after the request is processed, but before the view is rendered ( Controller After the method call) ");

  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>> Is called at the end of the entire request DispatcherServlet
    //  Execute after rendering the corresponding view (mainly for resource cleaning) ");
  }
}

Interceptor usage: I'm using @Component for annotations and it's also possible to declare configuration


@Component
public class ApplicationConfig {extends WebMvcConfigurerAdapter 

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //  Multiple interceptors 1 Three interceptor chains 
    // addPathPatterns  Used to add intercepting rules 
    // excludePathPatterns  User exclusion interception 
    registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**");
    super.addInterceptors(registry);
  }
}

Filter:

Definition:


public class ActionFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    //  Acquisition of system time 
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    //  Set a limit on the running time  0-4 point 
    if (hour < 4) {
      HttpServletResponse httpResponse = (HttpServletResponse) response;
      httpResponse.setCharacterEncoding("UTF-8");
      httpResponse.setContentType("application/json; charset=utf-8");
      
      //  The message 
      Map<String, Object> messageMap = new HashMap<>();
      messageMap.put("status", "1");
      messageMap.put("message", " This interface can request at a time of :0-4 point ");
      ObjectMapper objectMapper=new ObjectMapper();
      String writeValueAsString = objectMapper.writeValueAsString(messageMap);
      response.getWriter().write(writeValueAsString);
      
    } else {
      chain.doFilter(request, response);
    }

  }

  @Override
  public void destroy() {

  }

}

Use:


@Component
public class ApplicationConfig { 


  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    ActionFilter actionFilter = new ActionFilter();
    registrationBean.setFilter(actionFilter);
    List<String> urlPatterns = new ArrayList<String>();
    urlPatterns.add("/service/extract/json/*");
    registrationBean.setUrlPatterns(urlPatterns);
    return registrationBean;
  }
  

}

Related articles: