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

}

 

posted @ 2017-02-21 08:53  玲珑骰子安红豆  阅读(11693)  评论(0编辑  收藏  举报