一文详解Java拦截器与过滤器的使用
流程图
拦截器vs过滤器
拦截器是SpringMVC的技术
过滤器的Servlet的技术
先过过滤器,过滤器过完才到DispatcherServlet;
拦截器归属于SpringMVC,只可能拦SpringMVC的东西;
拦截器说白了就是为了增强,可以在请求前进行增强,也可以在请求后进行增强,但是不一定同时在前后都增强。
区别
SpringMVC技术架构图
项目Demo
依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Interceptor拦截器
package com.test.filtertest.inter; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 拦截器 * * @author: zhangzengxiu * @createDate: 2022/4/22 */ @Component public class TestInter1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("TestInter1 preHandle..."); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("TestInter1 postHandle..."); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("TestInter1 afterCompletion..."); } }
Interceptor拦截器要注册
package com.test.filtertest.config; import com.test.filtertest.inter.TestInter1; import com.test.filtertest.inter.TestInter2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author zhangzengxiu * @date 2022/4/22 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private TestInter1 testInter1; @Autowired private TestInter2 testInter2; @Override public void addInterceptors(InterceptorRegistry registry) { //默认/** 拦截所有请求 registry.addInterceptor(testInter1).addPathPatterns("/**"); registry.addInterceptor(testInter2); } }
拦截顺序
多拦截器拦截顺序取决于registry的注册顺序
Filter过滤器
1、多Filter不指定过滤顺序
package com.test.filtertest.filter; import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * @author zhangzengxiu * @date 2022/4/22 */ @WebFilter(filterName = "TestFilter1", urlPatterns = "/*") //@Order(2) //filter这样定义拦截顺序无效,还是会按照类名去拦截 public class TestFilter1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("TestFilter1 filter before ..."); filterChain.doFilter(servletRequest, servletResponse); System.out.println("TestFilter1 filter after ..."); } @Override public void destroy() { } }
加了@WebFilter注解的过滤器要生效,需要在启动类上加一个注解@ServletComponentScan
2、多Filter指定过滤顺序
如果要设置过滤顺序的话,在Filter上添加@Order 不会生效,还是会默认根据Filter过滤器的类名去进行过滤。
如果想要指定拦截顺序,需要添加一个配置类,通过设置Order的值去设置过滤顺序,Order数值越小优先级越高
package com.test.config; import com.test.filter.TestFilter1; import com.test.filter.TestFilter2; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; /** * filter过滤器配置 * * @author: zhangzengxiu * @createDate: 2022/4/24 */ @Configuration public class FilterConfig { @Bean public FilterRegistrationBean testFilter1() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); Filter testFilter1 = new TestFilter1(); filterRegistrationBean.setFilter(testFilter1); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.setName("testFilter1"); filterRegistrationBean.setOrder(2);//指定过滤顺序,数值越小优先级越高 return filterRegistrationBean; } @Bean public FilterRegistrationBean testFilter2() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); Filter testFilter2 = new TestFilter2(); filterRegistrationBean.setFilter(testFilter2); filterRegistrationBean.setName("testFilter2"); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.setOrder(1);//指定过滤顺序,数值越小优先级越高 return filterRegistrationBean; } }
1、同时原本Filter过滤器上的@WebFilter(filterName = “TestFilter1”, urlPatterns = “/*”)
这个注解最好去掉,否则bean重复会报错,要么bean名称不要重复,换个名称。但是不去掉的话@ServletComponentScan也要被保留,最好统一格式,只使用config配置。
2、启动类上的@ServletComponentScan 注解也不需要了,可以去掉了,因为不用扫描@WebFilter注解了。但是如果@WebFilter还保留了,那么就不能去掉了。不然加了@WebFilter的过滤器就不会生效了,因为没扫到。
到此这篇关于一文详解Java拦截器与过滤器的使用的文章就介绍到这了,更多相关Java拦截器 过滤器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring中@Controller和@RestController的区别详解
这篇文章主要介绍了Spring中@Controller和@RestController的区别详解,@RestController 是 @Controller 和 @ResponseBody 的结合体,单独使用 @RestController 的效果与 @Controller 和 @ResponseBody 二者同时使用的效果相同,需要的朋友可以参考下2023-10-10springboot logback调整mybatis日志级别无效的解决
这篇文章主要介绍了springboot logback调整mybatis日志级别无效的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-10-10
最新评论