Spring 整合过滤器

过滤器Filter是Servlet的一个技术,可通过过滤器对请求进行拦截,比如读取session判断用户是否登录、判断访问的请求URL是否有权限。

1、使用@WebFilter注解

controller

Spring 整合过滤器_第1张图片

 filter

Spring 整合过滤器_第2张图片

过滤拦截:

Spring 整合过滤器_第3张图片

3、启动类上加注解@ServletComponentScan

Spring 整合过滤器_第4张图片

 

2、多个filter

 filter1:

Spring 整合过滤器_第5张图片

 

filter2:

同 filter1

confiiguration:

Spring 整合过滤器_第6张图片

 

Spring 整合过滤器_第7张图片

 多个过滤器指定顺序两种方式:

  1、FilterRegistrationBeanspringboot提供的,此类提供setOrder方法,可以为filter设置排序值,让spring在注册web filter之前排序后再依次注册。

  2、通过过滤器的名字,进行顺序的约定,比如LogFilterAuthFilter,此时AuthFilter就会比LogFilter先执行,因为首字母AL前面。

多个过滤器器时,filterChain.doFilter(servletRequest, servletResponse); 递归执行。

 

 参考:http://www.importnew.com/29401.html

转载于:https://www.cnblogs.com/natian-ws/p/10822461.html

你可能感兴趣的:(Spring 整合过滤器)