服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring boot实现过滤器和拦截器demo

spring boot实现过滤器和拦截器demo

2020-08-18 11:37玲珑骰子安红豆 Java教程

本篇文章主要介绍了spring boot实现过滤器和拦截器demo ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

整理文档,搜刮出一个spring boot实现过滤器拦截器demo ,稍微整理精简一下做下分享。

拦截器定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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  其实也可能声明成配置

?
1
2
3
4
5
6
7
8
9
10
11
12
@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);
  }
}

过滤器:

定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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() {
 
  }
 
}

使用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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;
  }
  
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/erbiaoge/p/6422414.html

延伸 · 阅读

精彩推荐