溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

利用springboot如何實(shí)現(xiàn)一個(gè)過(guò)濾器功能

發(fā)布時(shí)間:2020-11-10 16:58:25 來(lái)源:億速云 閱讀:189 作者:Leah 欄目:編程語(yǔ)言

利用springboot如何實(shí)現(xiàn)一個(gè)過(guò)濾器功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

攔截器定義:

@WebServlet
public class ActionInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>>在請(qǐng)求處理之前進(jìn)行調(diào)用(Controller方法調(diào)用之前)");

    // 獲取系統(tǒng)時(shí)間
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    // 設(shè)置限制運(yùn)行時(shí)間 0-4點(diǎn)
    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>>>>>>>請(qǐng)求處理之后進(jìn)行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)");

  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>>在整個(gè)請(qǐng)求結(jié)束之后被調(diào)用,也就是在DispatcherServlet
    // 渲染了對(duì)應(yīng)的視圖之后執(zhí)行(主要是用于進(jìn)行資源清理工作)");
  }
}

攔截器使用:  關(guān)于注解 我使用的是@Component  其實(shí)也可能聲明成配置

@Component
public class ApplicationConfig {extends WebMvcConfigurerAdapter 

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    // 多個(gè)攔截器組成一個(gè)攔截器鏈
    // addPathPatterns 用于添加攔截規(guī)則
    // excludePathPatterns 用戶(hù)排除攔截
    registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**");
    super.addInterceptors(registry);
  }
}

過(guò)濾器:

定義:

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 {
    // 獲取系統(tǒng)時(shí)間
    Calendar ca = Calendar.getInstance();
    int hour = ca.get(Calendar.HOUR_OF_DAY);
    // 設(shè)置限制運(yùn)行時(shí)間 0-4點(diǎn)
    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", "此接口可以請(qǐng)求時(shí)間為:0-4點(diǎn)");
      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;
  }
  

}

看完上述內(nèi)容,你們掌握利用springboot如何實(shí)現(xiàn)一個(gè)過(guò)濾器功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI