Python 官方文档:入门教程 => 点击学习
目录流程图拦截器vs过滤器springMVC技术架构图项目Demo依赖Interceptor拦截器Filter过滤器1、多Filter不指定过滤顺序2、多Filter指定过滤顺序流程
过滤器的Servlet的技术
先过过滤器,过滤器过完才到DispatcherServlet;
拦截器归属于SpringMVC,只可能拦SpringMVC的东西;
拦截器说白了就是为了增强,可以在请求前进行增强,也可以在请求后进行增强,但是不一定同时在前后都增强。
区别
项目地址
<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>
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;
@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;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private TestInter1 testInter1;
@Autowired
private TestInter2 testInter2;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//默认
@WebFilter(filterName = "TestFilter1", urlPatterns = "
@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拦截器 过滤器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 一文详解Java拦截器与过滤器的使用
本文链接: https://lsjlt.com/news/147453.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0