spring boot 필터와 차단기 데모 구현

3869 단어 springboot
문서를 정리하고springboot 구현 필터와 차단기 데모를 스크랩하여 공유를 간소화합니다.
차단기 정의:

@WebServlet
public class ActionInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>> (Controller )");

    //  
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    //   0-4 
    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>>>>>>> , (Controller )");

  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>> , DispatcherServlet
    //  ( )");
  }
}
차단기 사용: 메모에 대해 @Component를 사용합니다. 사실 설정으로 설명할 수도 있습니다.

@Component
public class ApplicationConfig {extends WebMvcConfigurerAdapter 

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //  
    // addPathPatterns  
    // excludePathPatterns  
    registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**");
    super.addInterceptors(registry);
  }
}
여과기:
정의:

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 {
    //  
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    //   0-4 
    if (hour < 4) {
      HttpServletResponse httpResponse = (HttpServletResponse) response;
      httpResponse.setCharacterEncoding("UTF-8");
      httpResponse.setContentType("application/json; charset=utf-8");
      
      //  
      Map<String, Object> messageMap = new HashMap<>();
      messageMap.put("status", "1");
      messageMap.put("message", " :0-4 ");
      ObjectMapper objectMapper=new ObjectMapper();
      String writeValueAsString = objectMapper.writeValueAsString(messageMap);
      response.getWriter().write(writeValueAsString);
      
    } else {
      chain.doFilter(request, response);
    }

  }

  @Override
  public void destroy() {

  }

}

사용:

@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;
  }
  

}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기