在前面我们讲过拦截器怎么使用,参考:拦截器
和拦截器有个差不多的叫过滤器。
首先看一下下面的流程图:
如上图,其中prehandel、posthandel、afterCompletion是拦截器的方法,filter pre和filter after是过滤器的方法。
两者区别主要有以下几点:
基于以上区别,过滤器和拦截器的使用场景通常不同。
拦截器本质上是面向切面编程(AOP),符合横切关注点的功能都可以放在拦截器中来实现。
拦截器主要应用场景:
过滤器主要应用场景:
有以下两种方式使用过滤器。
先定义Filter:只要实现接口import javax.servlet.Filter
,并实现是哪个方法即可,其中最重要的是doFilter方法,在里面写我们的过滤逻辑。代码如下:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// doFilter()方法中的servletRequest参数的类型是ServletRequest,需要转换为HttpServletRequest类型方便调用某些方法
System.out.println("filter1");
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String ip = request.getRemoteAddr();
String url = request.getRequestURL().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String date = sdf.format(d);
System.out.printf("%s %s 访问了 %s%n", date, ip, url);
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
然后注册我们的Filter:
import com.example.server.common.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
// 设置过滤的url
filterRegistrationBean.addUrlPatterns("/*");
// 设置优先级
filterRegistrationBean.setOrder(1);
return filterRegistrationBean;
}
}
用@WebFilter就可以进行配置。代码如下:
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Component
// 定义filterName 和过滤的url
@WebFilter(filterName = "myFilter2" ,urlPatterns = "/*")
@Order(2)
public class MyFilter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("my filter2");
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
这里需要注意一点的是@WebFilter这个注解是Servlet3.0的规范,并不是Spring boot提供的。除了这个注解以外,我们还需在启动类中加另外一个注解:@ServletComponetScan,指定扫描的包。
@SpringBootApplication
@ServletComponentScan
public class FilterTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(FilterTestApplication.class, args);
}
}
参考文章:拦截器
参考文章:
https://blog.csdn.net/qq_49313444/article/details/107504196
https://blog.csdn.net/qq_42076204/article/details/125215984