spring boot過濾器和攔截器 demo


攔截器定義:

 1 @WebServlet
 2 public class ActionInterceptor implements HandlerInterceptor {
 3 
 4     @Override
 5     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
 6             throws Exception {
 7         // System.out.println(">>>MyInterceptor1>>>>>>>在請求處理之前進行調用(Controller方法調用之前)");
 8 
 9         // 獲取系統時間
10         Calendar ca = Calendar.getInstance();
11         int hour = ca.get(Calendar.HOUR_OF_DAY);
12         // 設置限制運行時間 0-4點
13         if (hour < 4) {
14             return true;
15         }
16         return false;
17     }
18 
19     @Override
20     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
21             ModelAndView modelAndView) throws Exception {
22         // System.out.println(">>>MyInterceptor1>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)");
23 
24     }
25 
26     @Override
27     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
28             throws Exception {
29         // System.out.println(">>>MyInterceptor1>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet
30         // 渲染了對應的視圖之后執行(主要是用於進行資源清理工作)");
31     }
32 }

攔截器使用:  關於注解 我使用的是@Component  其實也可能聲明成配置

 1 @Component
 2 public class ApplicationConfig {extends WebMvcConfigurerAdapter 
 3 
 4     @Override
 5     public void addInterceptors(InterceptorRegistry registry) {
 6         // 多個攔截器組成一個攔截器鏈
 7         // addPathPatterns 用於添加攔截規則
 8         // excludePathPatterns 用戶排除攔截
 9         registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**");
10         super.addInterceptors(registry);
11     }
12 }

 

 

過濾器:

定義:

 1 public class ActionFilter implements Filter {
 2 
 3     @Override
 4     public void init(FilterConfig filterConfig) throws ServletException {
 5 
 6     }
 7 
 8     @Override
 9     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
10             throws IOException, ServletException {
11         // 獲取系統時間
12         Calendar ca = Calendar.getInstance();
13         int hour = ca.get(Calendar.HOUR_OF_DAY);
14         // 設置限制運行時間 0-4點
15         if (hour < 4) {
16             HttpServletResponse httpResponse = (HttpServletResponse) response;
17             httpResponse.setCharacterEncoding("UTF-8");
18             httpResponse.setContentType("application/json; charset=utf-8");
19             
20             // 消息
21             Map<String, Object> messageMap = new HashMap<>();
22             messageMap.put("status", "1");
23             messageMap.put("message", "此接口可以請求時間為:0-4點");
24             ObjectMapper objectMapper=new ObjectMapper();
25             String writeValueAsString = objectMapper.writeValueAsString(messageMap);
26             response.getWriter().write(writeValueAsString);
27             
28         } else {
29             chain.doFilter(request, response);
30         }
31 
32     }
33 
34     @Override
35     public void destroy() {
36 
37     }
38 
39 }

使用:

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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM