亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot實(shí)現(xiàn)過濾器和攔截器demo

 更新時(shí)間:2017年02月23日 16:19:09   作者:玲瓏骰子安紅豆  
本篇文章主要介紹了spring boot實(shí)現(xiàn)過濾器和攔截器demo ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

整理文檔,搜刮出一個(gè)spring boot實(shí)現(xiàn)過濾器和攔截器demo ,稍微整理精簡一下做下分享。

攔截器定義:

@WebServlet
public class ActionInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    // System.out.println(">>>MyInterceptor1>>>>>>>在請求處理之前進(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>>>>>>>請求處理之后進(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è)請求結(jié)束之后被調(diào)用,也就是在DispatcherServlet
    // 渲染了對應(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 用戶排除攔截
    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 {
    // 獲取系統(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", "此接口可以請求時(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring配置文件中util:properties和context:property-placeholder用法

    spring配置文件中util:properties和context:property-placeholder用法

    這篇文章主要介紹了spring配置文件中util:properties和context:property-placeholder用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java基礎(chǔ)類Class使用指南

    Java基礎(chǔ)類Class使用指南

    關(guān)于通過類名訪問class屬性,我朋友問過好幾次了,一直沒明白這個(gè)東西到底是什么?對此,我參照網(wǎng)友們的博客,總結(jié)了一些小知識,如發(fā)現(xiàn)錯(cuò)誤,希望糾正,謝謝
    2015-12-12
  • SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出

    SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出功能的相關(guān)知識,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下
    2024-03-03
  • spring boot 2整合swagger-ui過程解析

    spring boot 2整合swagger-ui過程解析

    這篇文章主要介紹了spring boot 2整合swagger-ui過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java獲取http和https協(xié)議返回的json數(shù)據(jù)

    Java獲取http和https協(xié)議返回的json數(shù)據(jù)

    本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個(gè)方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。
    2017-01-01
  • 在Java8中如何避開空指針異常

    在Java8中如何避開空指針異常

    這篇文章主要給大家介紹了關(guān)于在Java8中如何風(fēng)騷走位的避開空指針異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 18個(gè)Java8日期處理的實(shí)踐(太有用了)

    18個(gè)Java8日期處理的實(shí)踐(太有用了)

    這篇文章主要介紹了18個(gè)Java8日期處理的實(shí)踐(太有用了),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • mybatis?log4j2打印sql+日志實(shí)例代碼

    mybatis?log4j2打印sql+日志實(shí)例代碼

    在學(xué)習(xí)mybatis的時(shí)候,如果用log4j2來協(xié)助查看調(diào)試信息,則會大大提高學(xué)習(xí)的效率,加快debug速度,下面這篇文章主要給大家介紹了關(guān)于mybatis?log4j2打印sql+日志的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Java實(shí)現(xiàn)記事本功能

    Java實(shí)現(xiàn)記事本功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)記事本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java源碼閱讀之java.lang.Object

    java源碼閱讀之java.lang.Object

    這篇文章主要介紹了java源碼閱讀之java.lang.Object,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評論